meta confusion new.instance() argument 1 must be classobj, not type

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?
 
R

Roeland Rengelink

Brad said:
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 don't know if it's a bug, but I think this alternative will work.

i = object.__new__(_new_shipment_factory)
i.__dict__.update(db_shipment.__dict__)
return i

Hope this help,

--
Roeland Rengelink

Author of PyORQ (http://pyorq.sourceforge.net), an Object-Relational
binding that lets you write queries as Python expression

'half of what I say is nonsense, unfortunately I don't know which half'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top