Python 3 Patterns & Idioms / code / PythonForProgrammers / ForgottenInitialization.py

# QuickPython/ForgottenInitialization.py

class Base(object):
     def __init__(self, x):
          self.x = x
     def f(self):
          print(self.x)

class Derived1(Base):
     # Base __init__() is passed through
     pass

Derived1(11).f()

class Derived2(Base):
     def __init__(self, y):
          "Init redefined, but forgot to call base-class init"
          self.y = y
     def f2(self):
          print(self.y)

d = Derived2(22)
d.f2()
try:
     d.f()
except:
     print "call to f() failed"

class Derived3(Base):
     def __init__(self, y):
          "Base-class call remembered"
          super(Derived3, self).__init__(y)

d = Derived3(33)
d.f()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.