confusion about __getattr__

Hans Nowak wurmy at earthlink.net
Wed Jan 16 22:45:00 EST 2002


Russ Cox wrote:
> 
> Could someone explain the error message in the
> following to me?  I apologize if it should be
> obvious, but clearly I have the wrong mental
> picture of what's going on.
> 
> g% python
> Python 2.2+ (#0, Jan 16 2002, 19:28:19) [C] on unknown
> Type "help", "copyright", "credits" or "license" for more information.
> >>> class Test:
> ...     def __getattr__(self, name):
> ...             return '__getattr__ for '+name
> ...     def __init__(self):
> ...             self.foo = 'bar'
> ...     def asdf(self):
> ...             return 'jkl;'
> ...
> >>> t=Test()
> >>> t.foo
> 'bar'
> >>> t.sdfsdfdsf
> '__getattr__ for sdfsdfdsf'
> >>> t.asdf()
> 'jkl;'
> >>> t.asdf
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: 'str' object is not callable
> >>>
> 
> What is the str object in question that isn't callable?

Strange. If I do this in *2.1*, then I get:

>>> t.asdf
<method Test.asdf of Test instance at 01378AF4>

...so I guess something must have changed in 2.2. I
don't have 2.2 installed myself (yet); are you
using the final version, not any betas or CVS
versions? I don't know if this behavior is normal
or not.

Ignorant-about-2.2-ly y'rs,

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



More information about the Python-list mailing list