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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    431
  2. Replies:
    3
    Views:
    425
    James Kanze
    Jul 16, 2008
  3. Replies:
    2
    Views:
    532
  4. Replies:
    5
    Views:
    389
    Kai-Uwe Bux
    Oct 12, 2008
  5. Igor R.

    Metaprogramming question

    Igor R., Jan 3, 2012, in forum: C++
    Replies:
    1
    Views:
    291
    Igor R.
    Jan 3, 2012
  6. Luc Juggery
    Replies:
    5
    Views:
    298
    Luc Juggery
    Oct 24, 2006
  7. Pedro Del Gallego

    Metaprogramming and module question

    Pedro Del Gallego, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    168
    Rick DeNatale
    Mar 6, 2007
  8. Travis Griggs

    Metaprogramming question

    Travis Griggs, Feb 11, 2014, in forum: Python
    Replies:
    0
    Views:
    133
    Travis Griggs
    Feb 11, 2014
Loading...