classification
Title: Explain __method__ lookup semantics for new-style classes
Type: Stage:
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bethard, georg.brandl, rhettinger
Priority: normal Keywords:

Created on 2007-03-21 08:53 by georg.brandl, last changed 2007-09-05 13:37 by georg.brandl. This issue is now closed.

Messages (5)
msg31600 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-21 08:53
__method__s (and next) are looked up on the type, not the instance. This isn't documented properly.
msg31601 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2007-04-03 22:01
This is not true for all __special__ methods, e.g. __enter__ and __exit__:

>>> class C(object):
...     pass
...    
>>> def enter(*args):
...     print 'enter', args
...     
>>> def exit(*args):
...     print 'exit', args
...     
>>> c = C()
>>> c.__enter__ = enter
>>> c.__exit__ = exit
>>> with c:
...     print 'hi'
...     
enter ()
hi
exit (None, None, None)

The documentation should say something like "When interpreting syntax that invokes a __special__ method, Python looks for the __special__ method on the instance's class. As an implementation detail, the lookup for some __special__ methods may also check the instance first, but this behavior should not be relied upon." This should probably go into the Reference Manual section 3.4: http://docs.python.org/ref/specialnames.html
msg31602 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-04-04 00:21
When writing docs, always cover the general case first and thoroughly.
Keep the recommendations constructive, positive and useful (as opposed
to wording like "this behavior should not be relied upon".  A doc
patch needs to add clarity -- if it doesn't, leave it out.
msg31603 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2007-04-04 01:13
Not sure exactly what you're proposing Raymond, but you could cut mine to "When interpreting syntax that invokes a __special__ method, Python looks for the __special__ method on the instance's class" if that's what you want.
msg55662 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-09-05 13:37
Added a paragraph in rev. 57992, 57993.
History
Date User Action Args
2007-09-05 13:37:04georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg55662
2007-03-21 08:53:45georg.brandlcreate