dunder-docs (was Python is DOOMED! Again!)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Feb 2 07:43:46 EST 2015


Rustom Mody wrote:

> My point was more methodological/sociological than technical:
> 
> Are these dunder methods as 'internal' as say the register-allocation used
> by a C compiler?


Dunder methods are implementation, not interface. If you are the class
author, then you care about the implementation, and write dunder methods.
But as the class user, you should not call dunder methods directly, instead
always go through the public interface:

# Not these.
a.__dir__()
seq.__len__()
x.__add__(y)
spam.__eq__(ham)

# Use these
dir(a)
len(seq)
x + y
spam == ham



-- 
Steven




More information about the Python-list mailing list