New-style classes and special methods

Carl Banks imbosol-1046070601 at aerojockey.com
Mon Feb 24 08:13:44 CET 2003


Jp Calderone wrote:
> [-- text/plain, encoding quoted-printable, 37 lines --]
> 
>  Consider this class:
> 
>    class Foo:
>        def __getattr__(self, name):
>            return lambda arg: arg
> 
>  It can be used thusly:
> 
>    f = Foo()
>    print f[10]

Sure?


>  Producing the output "10".  Now consider this class:
> 
>    class NewFoo(object):
>        def __getattr__(self, name):
>            return lambda arg: arg
> 
>  When used in the same way, this exception is raised:
> 
>    TypeError: unindexable object

It might have worked if you had used __getitem__ instead of
__getattr__.  At least it did for me.


>  From a quick glance at the source, it seems that neither tp_as_mapping nor
> tp_as_sequence are being initialized, presumably because __getitem__ isn't
> seen as being defined.

Correct.


>  Is this analysis of the behavior correct?  If so, what are the chances of
> this being "fixed"?
> 
>  Jp
> 

-- 
CARL BANKS




More information about the Python-list mailing list