__getattr__ feature

Hans Nowak wurmy at earthlink.net
Tue Jun 25 01:33:17 EDT 2002


I didn't know this was possible:

class Foo:
     def __init__(self, obj):
         self.obj = obj
     def __getattr__(self, name):
         return getattr(self.obj, name)

foo = Foo([1,2,3])
print foo[2]    # prints 3

I would expect the code to raise an exception, since there is no __getitem__ 
method. A quick test learns that this is apparently a feature new to 2.2;
it doesn't work if I try it in 2.1.

Interestingly, using getattr() directly on a list does *not* work (although 
this is what the method does):

 >>> x = [1, 2, 3]
 >>> getattr(x, 1)
Traceback (most recent call last):
   File "<pyshell#1>", line 1, in ?
     getattr(x, 1)
TypeError: attribute name must be string
 >>> getattr(x, "1")
Traceback (most recent call last):
   File "<pyshell#2>", line 1, in ?
     getattr(x, "1")
AttributeError: 'list' object has no attribute '1'

I didn't find anything about this in the reference manual. Does anybody know 
how/why this works and why it was added?

Cheers,

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/




More information about the Python-list mailing list