B
Brad Clements
I need to dynamically create a new type of class at runtime, based on an
oldstyle class and a new style class.
I'm using Python 2.3.2
My code:
def shipment_from_db_instance(db_shipment):
"""
Create a new shipment object from a db_shipment instance (old style)
"""
global _new_shipment_factory
if not _new_shipment_factory:
# create a new shipment factory dynamically
_new_shipment_factory = type('NewShipment', (Shipment,
db_shipment.__class__), {})
print "factory is ", _new_shipment_factory,
type(_new_shipment_factory)
return new.instance(_new_shipment_factory, db_shipment.__dict__)
But I get this output:
factory is <class 'MurkWorks.Shipments.Shipment.NewShipment'> <type 'type'>
File "/home/bkc/src/Python/MurkWorks/Shipments/Shipment.py", line 81, in
shipment_from_db_instance
return new.instance(_new_shipment_factory, db_shipment.__dict__)
TypeError: instance() argument 1 must be classobj, not type
It seems that new.instance() doesn't understand how to make instances from
types.
I tried this interactively and got the same error.
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: instance() argument 1 must be classobj, not type
What am I doing wrong, or is this a bug?
oldstyle class and a new style class.
I'm using Python 2.3.2
My code:
def shipment_from_db_instance(db_shipment):
"""
Create a new shipment object from a db_shipment instance (old style)
"""
global _new_shipment_factory
if not _new_shipment_factory:
# create a new shipment factory dynamically
_new_shipment_factory = type('NewShipment', (Shipment,
db_shipment.__class__), {})
print "factory is ", _new_shipment_factory,
type(_new_shipment_factory)
return new.instance(_new_shipment_factory, db_shipment.__dict__)
But I get this output:
factory is <class 'MurkWorks.Shipments.Shipment.NewShipment'> <type 'type'>
File "/home/bkc/src/Python/MurkWorks/Shipments/Shipment.py", line 81, in
shipment_from_db_instance
return new.instance(_new_shipment_factory, db_shipment.__dict__)
TypeError: instance() argument 1 must be classobj, not type
It seems that new.instance() doesn't understand how to make instances from
types.
I tried this interactively and got the same error.
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: instance() argument 1 must be classobj, not type
What am I doing wrong, or is this a bug?