[Python-3000] __special__ attrs looked up on the type, not instance
Guido van Rossum
guido at python.org
Wed Mar 14 19:37:37 CET 2007
On 3/14/07, Nick Coghlan <ncoghlan at gmail.com> wrote:
> > 1) I didn't remember this, do we have it documented somewhere?
> > 2) Assuming #1 is correct, is this rule consistently applied?
> > 3) How does (should) this affect 2.6 and migration to 3.0, if at all?
> The principle isn't consistently applied - the with statement generates
> standard GET_ATTR opcodes, so it checks the instance first when looking
> for __enter__ and __exit__.
> It was questioned at the time, and Guido was OK with it - I believe his
> position was that defining special methods on instances may or may not
> affect behaviour, and whether or not it does so is implementation dependent.
Right. At the same time, we could be more rigorous in 3.0 if we wanted
to. We could even do this by hacking the default getattr
implementation to skip the instance dict if the name starts and ends
with two underscores.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000