Copy constructors
Andrew Kuchling
akuchlin at mems-exchange.org
Mon Aug 13 08:19:32 EDT 2001
I have an example where being unable to assign __class__ is a problem.
ExtensionClass already has this limitation today; you can assign
extclass.__class__, but that just adds an __class__ key to the
instance dictionary and doesn't really affect the object's class.
Now, consider a collection of instances in a ZODB. You redesign your
objects a little, and want to change instances of class A into class B
or C. If you can assign to __class__, this is easily done, just a
matter of converting each instances as you hit it. I imagine this
could even be done in a rather sneaky __setstate__.
Without __class__ assignment, the job is much messier because you have
to create a new instance that will get a new OID. Any object in the
database may have a reference to a given instance of A and such
references need to be preserved, so now you have to walk all the
objects to find and fix all the references to A instances. Lack of
dynamicism and persistent databases don't mix very well.
--amk
More information about the Python-list
mailing list