By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

newstyle classes and __getattribute__

P: n/a
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.

Oct 28 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Oct 28 '05 #2

P: n/a
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/
Oct 28 '05 #3

P: n/a
James Stroud schrieb:
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

Sorry, but I am right that you explicitly call a "super"
__getattribute__ on object and pass it a reference to self and the
desired key ?
Only asking for clarification ...

But why does that work under 2.4.1, and even under ActiveState's 2.4.1 ?
Was that changed between those 2 releases ?
Intuitive behaviour of __getattribute__ would be:
If a key is not handeld in that function, return what you already got.

Cheers,
Stefan

Oct 28 '05 #4

P: n/a
James Stroud schrieb:
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

Sorry, but I am right that you explicitly call a "super"
__getattribute__ on object and pass it a reference to self and the
desired key ?
Only asking for clarification ...

But why does that work under 2.4.1, and even under ActiveState's 2.4.1 ?
Was that changed between those 2 releases ?
Intuitive behaviour of __getattribute__ would be:
If a key is not handeld in that function, return what you already got.

Cheers,
Stefan
Oct 28 '05 #5

P: n/a
Stefan Sonnenberg-Carstens schrieb:
James Stroud schrieb:
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

Sorry, but I am right that you explicitly call a "super"
__getattribute__ on object and pass it a reference to self and the
desired key ?
Only asking for clarification ...

But why does that work under 2.4.1, and even under ActiveState's 2.4.1 ?
Was that changed between those 2 releases ?
Intuitive behaviour of __getattribute__ would be:
If a key is not handeld in that function, return what you already got.

Cheers,
Stefan

Sorry,forget that.
It never worked in those releases,
I made some errors which made me belief that, sorry.
Oct 28 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.