newstyle classes and __getattribute__

James Stroud jstroud at mbi.ucla.edu
Fri Oct 28 17:42:51 EDT 2005


On Friday 28 October 2005 14:26, Stefan Sonnenberg-Carstens wrote:
> Hi there,
[..clip..]
> 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
[..snip..]
> But, then surprise:
>  >>> t = T(name="test123",port=443)
>  >>> dir(t)
>
> []
>
> What the hell is going wrong here ?

__getattribute__ is returning None in all cases and dir() is converting None 
to [].

Anyway, you should have done this:

py> class T(object):
...     def __init__(self,name='',port=80):
...             self.name=name
...     def __getattribute__(self,key):
...             if key=='somekey':
...                     return None
...             else:
...               return object.__getattribute__(self, key)
...
py> t = T(name="test123",port=443)
py> dir(t)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'name']

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list