converting base class instance to derived class instance

John Roth newsgroups at jhrothjr.com
Mon Feb 9 18:36:35 CET 2004


"François Pinard" <pinard at iro.umontreal.ca> wrote in message
news:mailman.1372.1076344361.12720.python-list at python.org...
[John Roth]

> If you want to do a little bit of deep magic, a factory function can
> create an instance by calling object(), plug in whatever attributes
> it wants and then change the __class__ attribute to whatever class it
> wants before it returns the newly minted instance. It doesn't have
> to go near anything that resembles a constructor (other than calling
> object() to get a new instance, of course.)

Hello, John, and gang! :-)

How one does that?  I'm merely curious.  Using Python 2.3.3, the result
of `object()' does not have a `__dict__', and seemingly may not be given
a `__dict__' either.  See:

[John's answer]
My goof. The correct call is:

object.__new__(klas)

where klas is the class object you want
the instance constructed for.


John Roth


-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list