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

Roeland Rengelink roeland.rengelink at chello.nl
Wed Jun 23 01:13:43 CEST 2004


Brad Clements wrote:
> 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'



More information about the Python-list mailing list