Method Underscores?

Josiah Carlson jcarlson at
Thu Oct 21 08:31:45 CEST 2004

"Chris S." <chrisks at> wrote:
> Is there a purpose for using trailing and leading double underscores for 
> built-in method names? My impression was that underscores are supposed 
> to imply some sort of pseudo-privatization, but would using 
> myclass.len() instead of myclass.__len__() really cause Python 
> considerable harm? As much as I adore Python, I have to admit, I find 
> this to be one of the language's most "unPythonic" features and a key 
> arguing point against Python. I've searched for a discussion on this 
> topic in the groups archives, but found little. What are everyone's 
> thoughts on this subject?

Double underscore methods are considered "magic" methods.  The
underscores are a hint that they may do something different.  Kind of
like the C++ friend operators.

In terms of .len() vs .__len__(), it is not supposed to be called
directly by user code; __len__() is called indirectly by the len()
builtin (and similarly for the other __<op>__() methods, check common
spellings in the operator module).

class foo:
    def __len__(self):
        return 4

a = foo()
len(a)    #like this

 - Josiah

More information about the Python-list mailing list