Metaprogramming question

S

Steve Menard

I have a need to create class instance without invokking the class' __init__
method.

Were I using old-style classes, I'd use new.instance() function. However, I
am using new-style classes and new.instance() complain "TypeError:
instance() argument 1 must be classobj, not type" ...

So my question is, how to replicate new.instance() functionality with new
classes?

Steve Menard
Author and Maintainer of http://jpype.sourceforge.net
 
M

MonkeeSage

Steve said:
So my question is, how to replicate new.instance() functionality with new
classes?

class A(object):
def __init__(self):
print "Class A"
A()
A.__new__(A) # <- this one

Regards,
Jordan
 
G

Georg Brandl

Steve said:
I have a need to create class instance without invokking the class' __init__
method.

Were I using old-style classes, I'd use new.instance() function. However, I
am using new-style classes and new.instance() complain "TypeError:
instance() argument 1 must be classobj, not type" ...

So my question is, how to replicate new.instance() functionality with new
classes?

Use object.__new__.

Georg
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top