Bypassing __setattr__ for changing special attributes

Facundo Batista facundo at taniquetil.com.ar
Tue Feb 27 07:11:41 EST 2007


Ziga Seilnacht wrote:


>>>> object.__setattr__(f, '__class__', Bar)
>>>> f.__class__ is Bar
> True

Interesting, but... why I must do this? And, I must *always* do this?

With Foo and Bar like the OP coded (just two new style classes, f is
instance of Foo), see this:

>>> f
<__main__.Foo object at 0xb7d1280c>
>>> setattr(f, '__class__', Bar)
>>> f
<__main__.Foo object at 0xb7d1280c>


Ok, didn't work, try your way:

>>> object.__setattr__(f, '__class__', Bar)
>>> f
<__main__.Bar object at 0xb7d1280c>


Wow! Ok, but getting back to Foo, with the *former* method:

>>> setattr(f, '__class__', Foo)
>>> f
<__main__.Foo object at 0xb7d1280c>


I can't explain this to myself, :(

Regards,

-- 
.   Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/






More information about the Python-list mailing list