__getattribute__ doesn't work on 'type' type for '__class__'
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue Jun 20 19:01:31 EDT 2006
Barry Kelly a écrit :
> I'm running this version of Python:
>
> Python 2.4.3 (#1, May 18 2006, 07:40:45)
> [GCC 3.3.3 (cygwin special)] on cygwin
>
> I read in the documentation that these two expressions are
> interchangeable:
>
> x.__getattribute__('name') <==> x.name
I wouldn't say they are interchangeable. FWIW, __getattribute__ starts
and ends with two underscores which in Python - and a lot of other
languages - has a very strong 'language internals, black magic stuff,
not intented for direct client code use'.
>
> Yet when I try this with the 'type' type, it doesn't work:
>
> ---8<---
>
>>>>x.__class__.__class__
>
> <type 'type'>
>
>>>>x.__class__.__getattribute__('__class__')
You're calling __getattribute__ on x.__class__, not on
x.__class__.__class__. But anyway, __getattribute__ is a descriptor,
which implies different behaviour when called on a class object. Please
read the doc on the descriptor protocol. For making long things short,
instance.__getattribute__('attrname')
translates to
klass.__getattribute__(instance, 'attrname')
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: descriptor '__getattribute__' requires a 'int' object but
> received a 'str'
I deduce from this that your 'x' is an int. Since you're calling
__getattribute__ on the type 'int', you have to pass an int as the first
parameter, ie:
>>> x = 1
>>> x.__class__.__getattribute__(x, '__class__')
<type 'int'>
Note BTW that it returns x.__class__, not x.__class__.__class__
More information about the Python-list
mailing list