This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author grodr
Date 2003-08-15.14:18:25
SpamBayes Score
Marked as misclassified
This came out from a thread in comp.lang.python: Here’s 
the reference:

Consider the following example (provided by M. 

>>> class M(type):
... 	def __getattr__(self, name):
... 		if name == '__iter__':
... 			return lambda self: iter([])
>>> class C(object):
... 	__metaclass__ = M
>>> C.__iter__
<function <lambda> at 0x0110E8F0>
>>> c = C()
>>> iter(c)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: iteration over non-sequence

This means that the iterator machinery does not check 
__getattr__ (or __getattribute__ for that matter – I 
have made the test). The Language Reference says:

“A class can implement certain operations that are 
invoked by special syntax (such as arithmetic operations 
or subscripting and slicing) by defining methods with 
special names.”

Which does not throw much light on the matter at hand. 
So there are two ways we can view the above:

(A) It’s a bug.

This is the one I favour ;-). Arguing by contradiction, 
not being considered a bug means that there is a very 
special distinction being made when it comes to 
attribute lookup of special names. 

I tend to follow the Gang of Four’s main 
injunction “Prefer composition to inheritance”. 
Composition is great in Python precisely because of the 
__getattr__ hook. Not being able to use __getattr__ in 
metaclasses to trap special names surely hampers that 
role somewhat.

(B) It’s not a bug.

Then at least I think that the documentation should be 
worded more accurately. Quoting A. Martelli on the same 

“__getattr__ is not a BINDING of the special method, 
though it may be considered a DEFINITION of it, which is 
why the current phrase in the Language Reference is not 
100% correct and complete -- only 99.44%, and I agree 
that the remaining 0.56% _is_ a delicate defect in the 

With my best regards,
G. Rodrigues
Date User Action Args
2007-08-23 14:16:07adminlinkissue789262 messages
2007-08-23 14:16:07admincreate