The __format__() method is used by f-strings, the str.format() method, as well as the format() built-in function. All three of these interfaces are used to create presentable string versions of a given object.
The following are the two ways in which arguments will be presented to the __format__() method of a someobject object:
- someobject.__format__(""): This happens when an application uses a string such as f"{someobject}", a function such as format(someobject), or a string formatting method such as "{0}".format(someobject). In these cases, there was no : in the format specification, so a default zero-length string is provided as the argument value. This should produce a default format.
- someobject.__format__(spec): This happens when an application uses a string such as f"{someobject:spec}", a function such as format...