Python variables are bound to types when used?

sjdevnull at yahoo.com sjdevnull at yahoo.com
Wed Oct 19 18:31:46 EDT 2005


Fredrik Lundh wrote:
> reset your brain:
>
>     http://effbot.org/zone/python-objects.htm

Neat link.

Can you expand on this:

> a type (returned by type(x))
...
> You cannot change the type.

Especially what's going on here:

>>> class a(object):
...     pass
...
>>> class b(a):
...     pass
...
>>> x=b()
>>> x.attr=1
>>> type(x)
<class '__main__.b'>
>>> type(y)
<class '__main__.b'>
>>> x.__class__=a
>>> type(x)
<class '__main__.a'>
>>> x.attr
1

It looks to me like x is still referencing the same object (and still
has the "attr" attribute) but its type has changed.  Is that not right?




More information about the Python-list mailing list