newstyle classes and __getattribute__

Stefan Sonnenberg-Carstens stefan.sonnenberg at freenet.de
Fri Oct 28 17:29:09 EDT 2005


Stefan Sonnenberg-Carstens schrieb:
> Hi there,
> I'm facing some strange things - but maybe only me is strange - anyway...
> 
> i wrote the following code:
> 
> 
> +++
> class T(object):
>     def __init__(self,name='',port=80):
>         self.name=name
>         self.port=port
> +++
> 
> looks fine - to me.
> 
> Now I can do:
> 
> 
>  >>> t = T(name="test",port=8080)
>  >>> print t
> <__main__.T object at 0x00BE36D0>
> 
>  >>> print t.name
> test
>  >>> dir(t)
> ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
> '__hash__', '__init__', '__module__', '__new__', '__reduce__', 
> '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 
> 'name', 'port']
> 
> Everything ok.
> 
> Now, I do this:
> 
> class T(object):
>     def __init__(self,name='',port=80):
>         self.name=name
>         self.port=port
>     def __getattribute__(self,key):
>         if key=='somekey':
>             return None
> 
> where
> 
>  >>> dir(T)
> ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
> '__hash__', '__init__', '__module__', '__new__', '__reduce__', 
> '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
> 
> is ok, also.
> 
> But, then surprise:
>  >>> t = T(name="test123",port=443)
>  >>> dir(t)
> []
> 
> What the hell is going wrong here ?
> I'm running Python 2.4.2 from python.org on Windows XP SP2, all patches 
> applied.
> 
> Thx in advance.
> 

Ok, to make it worse:
I tried the same under Cygwin with Python 2.4.1 -
it worked as expected.



More information about the Python-list mailing list