Yet another Python textbook

Steven D'Aprano steve+comp.lang.python at
Fri Nov 23 04:26:37 CET 2012

On Thu, 22 Nov 2012 17:41:22 -0500, Colin J. Williams wrote:

> You and I used __format__.  I understand that the use of double
> underscore functions is deprecated.

Double leading and trailing underscore methods are not deprecated, they 
are very much part of the public interface. But they are reserved for 
Python's use, and under normal conditions you should not be using them by 
hand. For example:

y = x.__add__(1)  # NO
y = x + 1  # YES

if mylist.__len__() > 2:  # NO
if len(mylist) > 2:  # YES

> Is there some regular function which can achieve the same result?

The built-in format() function is the public API for calling the dunder 
method __format__. So normally you would write:

format(value, spec)

instead of value.__format__(spec).


More information about the Python-list mailing list