Re: Python 2.3 compatibility?

Paul Moore <pf_moore-/[email protected]> Wed, 21 Jan 2004 18:42:12 +0000
Newsgroups gmane.comp.pythin.pyds.devel
Message-ID <[email protected]>
Bob Ippolito <bob-Zl9L/[email protected]> writes:

> Are you implementing __getattr__?  Because that behaves a lot
> differently for classic and new-style classes.
>
>  >>> class Foo:
> ...   def __getattr__(self, foo):
> ...     print foo
> ...
>  >>> f = Foo()
>  >>> f
> __repr__
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> TypeError: 'NoneType' object is not callable
>
>
>  >>> class F(object):
> ...   def __getattr__(self, foo):
> ...     print foo
> ...
>  >>>
>  >>> f = F()
>  >>> f
> <__main__.F object at 0x554d0>

The reason for this is that object implements __repr__. So in the
absence of a definition in F, F inherits object's definition. With the
"classic" class Foo, there is no inherited __repr__, so __getattr__
gets called (and returns None).

Paul
-- 
This signature intentionally left blank