Issue 1684991: Explain method lookup semantics for new-style classes (original) (raw)

Created on 2007-03-21 08:53 by georg.brandl, last changed 2022-04-11 14:56 by admin. 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
2022-04-11 14:56:23 admin set github: 44751
2007-09-05 13:37:04 georg.brandl set status: open -> closedresolution: fixedmessages: +
2007-03-21 08:53:45 georg.brandl create