__getattribute__ doesn't work on 'type' type for '__class__'
bdesth.quelquechose at free.quelquepart.fr
Wed Jun 21 01:01:31 CEST 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
> 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:
> <type 'type'>
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,
> 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
>>> x = 1
>>> x.__class__.__getattribute__(x, '__class__')
Note BTW that it returns x.__class__, not x.__class__.__class__
More information about the Python-list