Down casting Python objects

Rami Chowdhury rami.chowdhury at
Wed Mar 10 06:24:00 CET 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: <>

More information about the Python-list mailing list