Down casting Python objects

Rami Chowdhury rami.chowdhury at gmail.com
Wed Mar 10 00:24:00 EST 2010


On Mar 9, 2010, at 13:26 , Andrey Fedorov wrote:

> So I have `x', a instance of class `Foo'. I also have class `Bar', a class extending `Foo' with a couple of methods. I'd like to "down cast" x as efficiently as possible. Is it OK to just set `x.__class__ = Bar' and expect things to work OK in all major versions of CPython? 

Could you tell us *why* you need to down-cast x? Explicit type-casting is usually unnecessary in Python...

-------------
Rami Chowdhury
"Never assume malice when stupidity will suffice." -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100309/131c7d4b/attachment.html>


More information about the Python-list mailing list