Any gotchas in returning a subclass instance from __new__?

s.lipnevich at gmail.com s.lipnevich at gmail.com
Tue Aug 1 07:23:23 EDT 2006


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.




More information about the Python-list mailing list