[Python-3000] Code freeze?

Christian Heimes lists at cheimes.de
Fri Feb 29 14:09:51 CET 2008


Nick Coghlan wrote:
> The interpreter is actually permitted to bypass the instance when 
> looking up magic methods. Whether it does or not is implementation 
> dependent - even CPython varies its behaviour depending on the specific 
> circumstance (e.g. the translation of with statements to bytecode ends 
> up using normal GET_ATTR opcodes, so those will see __enter__ and 
> __exit__ methods on instances, but most of the magic method lookups from 
> C code bypass the instance and go straight to the relevant tp_* slot).

I suspected the different behavior is related to C type slots as well.
tp_iter is part of the PyTypeObject struct and the __enter__ and
__exit__ methods are ordinary methods. I just wasn't sure if the
behavior is correct. I've never encountered a problem with the dark
corners of the language so far. ;)

I did some further testing. iter() doesn't even go through
__getattribute__. It grabs the class and does a lookup on the class.

>>> class Example(object):
...     def __getattribute__(self, name):
...         print name
...         return object.__getattribute__(self, name)
>>> e = Example()
>>> iter(e)
__class__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Example' object is not iterable

That's either a bug or at least a big gotcha for developers of proxy
objects. At least I expected that __getattribute__ is called for *every*
attribute including magic methods.

Is this documented somewhere? The docs say "See the
:meth:`__getattribute__` method below for a way to actually get total
control over attribute access.".

Christian



More information about the Python-3000 mailing list