470,618 Members | 1,721 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,618 developers. It's quick & easy.

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

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

_


Jul 18 '05 #1
1 2399
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'
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Samuel Kleiner | last post: by
38 posts views Thread by Grant Edwards | last post: by
21 posts views Thread by ron | last post: by
6 posts views Thread by Kent Johnson | last post: by
10 posts views Thread by John M. Gabriele | last post: by
3 posts views Thread by Beta What | last post: by
3 posts views Thread by Wesley Brooks | last post: by
8 posts views Thread by vaib | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.