Advanced Dictionary

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Jun 16 12:34:59 EDT 2010


On Wed, 16 Jun 2010 09:17:47 -0700, Stephen Hansen wrote:

> Leading-and-trailing double underscores are explicitly reserved for
> Python to define as Special. 


That part is correct. But of course Python doesn't prevent you from 
ignoring this rule (more of a guideline really).



> They also imply a slightly different
> calling semantic: normal leading-and-trailing double underscore methods
> bypass instance lookup.

This is semi-correct. Operator overloading and other special method 
lookup bypasses instance lookup and calls type(instance).__method__ but 
that has nothing to do with the double-underscore methods themselves. 
They operate normally if you call them by hand:


>>> class C(object):
...     def __add__(self, other):
...             return "special"
...
>>> c = C()
>>> c.__add__ = lambda self, other: "not very special"
>>> c + 4
'special'
>>> c.__add__(c, 4)
'not very special'


-- 
Steven



More information about the Python-list mailing list