__getattribute__ doesn't work on 'type' type for '__class__'

Ziga Seilnacht ziga.seilnacht at gmail.com
Tue Jun 20 16:47:56 EDT 2006


Barry Kelly wrote:
[snipped]
> Yet when I try this with the 'type' type, it doesn't work:
>
> ---8<---
> >>> x.__class__.__class__
> <type 'type'>
> >>> x.__class__.__getattribute__('__class__')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: descriptor '__getattribute__' requires a 'int' object but
> received a 'str'
> --->8---
>
> Why is this?

The problem is that your class (I would guess that x is an int) and its
type have a method with the same name. As is normal for attribute
lookup, the instance's attribute is first looked up in its __dict__.
Since x.__class__  is a type, this results in __getattribute__
being an unbound method of that type. What you are doing
is similar to:

>>> L = ["spam", "eggs"]
>>> "".__class__.join(L)
Traceback (most recent call last):
  ...
TypeError: descriptor 'join' requires a 'str' object but received a
'list'

Which as you can see, fails with the same error message.

> -- Barry
> 
> -- 
> http://barrkel.blogspot.com/

Hope this helps,

Ziga




More information about the Python-list mailing list