Metaprogramming question

Discussion in 'Python' started by Steve Menard, Oct 6, 2006.

  1. Steve Menard

    Steve Menard Guest

    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
     
    Steve Menard, Oct 6, 2006
    #1
    1. Advertising

  2. Steve Menard

    MonkeeSage Guest

    Steve Menard wrote:
    > 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
     
    MonkeeSage, Oct 6, 2006
    #2
    1. Advertising

  3. Steve Menard

    Georg Brandl Guest

    Steve Menard wrote:
    > 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
     
    Georg Brandl, Oct 6, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    326
  2. Replies:
    3
    Views:
    350
    James Kanze
    Jul 16, 2008
  3. Replies:
    2
    Views:
    450
  4. Replies:
    5
    Views:
    324
    Kai-Uwe Bux
    Oct 12, 2008
  5. Igor R.

    Metaprogramming question

    Igor R., Jan 3, 2012, in forum: C++
    Replies:
    1
    Views:
    205
    Igor R.
    Jan 3, 2012
Loading...

Share This Page