Any gotchas in returning a subclass instance from __new__?

Discussion in 'Python' started by s.lipnevich@gmail.com, Aug 1, 2006.

  1. Guest

    Hi All,

    Is anything wrong with the following code?

    class Superclass(object):
    def __new__(cls):
    # Questioning the statement below
    return super(Superclass, cls).__new__(Subclass)
    class Subclass(Superclass):
    pass
    if __name__ == '__main__':
    instance = Superclass()
    print instance

    It works here, and even constructors for Subclass and Superclass are
    invoked (in correct order), even though such behavior is not explicitly
    stated here http://docs.python.org/ref/customization.html. So, am I
    asking for trouble or is it /the/ way to go about transforming base
    class into a factory?
    Thank you,

    Sergey.
    , Aug 1, 2006
    #1
    1. Advertising

  2. Duncan Booth Guest

    wrote:

    > Hi All,
    >
    > Is anything wrong with the following code?
    >
    > class Superclass(object):
    > def __new__(cls):
    > # Questioning the statement below
    > return super(Superclass, cls).__new__(Subclass)
    > class Subclass(Superclass):
    > pass
    > if __name__ == '__main__':
    > instance = Superclass()
    > print instance
    >

    The only problems I can think if is that if you create any other subclasses
    of Superclass, or of Subclass for that matter then you'll never succeed in
    creating any of them, and the constructor i.e. __new__ of the subclass
    never gets called.

    I've used this technique where the Superclass iterates through all of its
    subclasses and uses the constructor parameters to choose the most
    appropriate subclass to construct. Eventually though I changed it to use a
    separate factory function: that way the __new__ methods get called in the
    expected order.

    So something like:

    @classmethod
    def fromMimetype(cls, m):
    for subclass in Superclass._handlers():
    if subclass.supportsMimetype(m):
    return subclass(m)

    @classmethod
    def _handlers(cls):
    for c in cls.__subclasses__():
    for sub in c._handlers():
    yield sub
    yield c

    > It works here, and even constructors for Subclass and Superclass are
    > invoked (in correct order), even though such behavior is not explicitly
    > stated here http://docs.python.org/ref/customization.html. So, am I
    > asking for trouble or is it /the/ way to go about transforming base
    > class into a factory?
    > Thank you,
    >

    I think you meant the initialisers are invoked correctly (the constructor
    is the __new__ method): this isn't suprising since the system simply calls
    __init__ on the returned object so the correct thing just happens.
    Duncan Booth, Aug 1, 2006
    #2
    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. Steven Bethard
    Replies:
    2
    Views:
    443
    Steven Bethard
    Feb 16, 2005
  2. Gerry Sutton
    Replies:
    1
    Views:
    524
    Peter Otten
    Apr 16, 2005
  3. Sandra-24
    Replies:
    18
    Views:
    1,837
    Steve Holden
    Apr 29, 2006
  4. Ken Schutte

    modifying __new__ of list subclass

    Ken Schutte, Aug 15, 2006, in forum: Python
    Replies:
    5
    Views:
    316
    Ken Schutte
    Aug 15, 2006
  5. Aaron Brady

    subclass PyDictObject -- any gotchas?

    Aaron Brady, Jan 22, 2009, in forum: Python
    Replies:
    1
    Views:
    350
    Aaron Brady
    Jan 22, 2009
Loading...

Share This Page