[ python-Bugs-1684991 ] Explain __method__ lookup semantics for new-style classes

SourceForge.net noreply at sourceforge.net
Wed Apr 4 00:01:13 CEST 2007


Bugs item #1684991, was opened at 2007-03-21 02:53
Message generated for change (Comment added) made by bediviere
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1684991&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Documentation
Group: None
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Georg Brandl (gbrandl)
Assigned to: Nobody/Anonymous (nobody)
Summary: Explain __method__ lookup semantics for new-style classes

Initial Comment:
__method__s (and next) are looked up on the type, not the instance. This isn't documented properly.

----------------------------------------------------------------------

Comment By: Steven Bethard (bediviere)
Date: 2007-04-03 16:01

Message:
Logged In: YES 
user_id=945502
Originator: NO

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

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1684991&group_id=5470


More information about the Python-bugs-list mailing list