newstyle classes and __getattribute__

  • Thread starter Stefan Sonnenberg-Carstens
  • Start date
S

Stefan Sonnenberg-Carstens

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:

['__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
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']

is ok, also.

But, then surprise:[]

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.
 
S

Stefan Sonnenberg-Carstens

Stefan said:
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:

['__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
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']

is ok, also.

But, then surprise:[]

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.
 
J

James Stroud

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:
[]

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/
 
S

Stefan Sonnenberg-Carstens

James said:
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
 
S

Stefan Sonnenberg-Carstens

James said:
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
 
S

Stefan Sonnenberg-Carstens

Stefan said:
James said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top