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

Brad Clements bkc at Murkworks.com
Tue Jun 22 11:06:03 EDT 2004


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.

>>> class old: pass
...
>>> class new(object): pass
...
>>> c = type('combined', (new, old), {})
>>> c
<class '__main__.combined'>
>>> import new
>>> i = new.instance(c, {})
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?

-- 
Novell DeveloperNet Sysop #5

_







More information about the Python-list mailing list