Double underscore names

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Feb 12 17:39:23 EST 2008


Double-underscore names and methods are special to Python. Developers are 
prohibited from creating their own (although the language doesn't enforce 
that prohibition). From PEP 0008, written by Guido himself:

      __double_leading_and_trailing_underscore__: "magic" objects or
      attributes that live in user-controlled namespaces.  E.g. __init__,
      __import__ or __file__.  Never invent such names; only use them
      as documented.

http://www.python.org/dev/peps/pep-0008/



But then there are modules like doctest, which uses the special double-
underscore name __test__.

There are times where I would like to create my own protocol, like the 
early sequence protocol: if an object has a __getitem__ method, it can be 
used with for loops. Python calls obj.__getitem__(i) with i starting at 0 
and increasing by one each time until it gets an IndexError exception.

This sequence protocol has been made partly obsolete by the iterator 
protocol, but you see the point: in the spirit of duck typing, having the 
ability to create a protocol is a Very Good Thing, and double leading and 
trailing underscore names are the accepted Python style for such special 
methods.

But the style guide says not to do that.

So I find myself conflicted. I'm aware that the style guide also says, 
know when to break the rules, but the examples given don't seem to apply 
to this case. The prohibition against inventing new double-underscore 
names like __parrot__ seems much stronger than the other style guides.

So what do folks think? I believe the protocol idiom ("look for a method 
called __parrot__ and then do something with it") is too useful and 
powerful to be ignored, but then if __parrot__ is reserved by Python, 
what to do?



-- 
Steven



More information about the Python-list mailing list