Proposal: default __init__

Andrew Dalke dalke at acm.org
Mon Nov 13 09:27:30 EST 2000


Hello,

  I propose that objects in some Python 2.x or in 3K have a default
__init__.  As it is now, base classes must explicitly include an
__init__ to be future-proof, which is not obvious.  Let me explain.

  I updated one of my libraries and ended up having to make a lot
of changes to client code.  What happened was the base class didn't
have a constructor

  class Spam:
    pass

so my client code was declared like:

  class Eggs(Spam):
    def __init__(self):
       self.scrambled = 0

It could not call Spam.__init__(self) because that method does not exist.
But then the base class changed so it had a constructor:

  class Spam:
    def __init__(self):
      self.__cache = {}

That meant I needed to change all of my Egg's __init__s to call
Spam.__init__.

However, in some sense the API for Spam did not change.  Suppose
all classes have a built-in method called __init__ which takes no
parameters and does nothing but is called if there is no user defined
call.  Then I could have written

  class Eggs(Spam):
    def __init__(self):
      Spam.__init__(self)
      self.scrambled = 0

and not worried about
  class Spam:
    pass

having a different interface than the functionally identical
  class Spam:
    def __init__(self):
      pass

Comments?  Any reason why this would be a bad thing?

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list