Source

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

# QuickPython/Initialization.py
"""
For text:
1) Doesn't automatically call base class constructor except in 1 special case.
2) Doesn't force base-class constructor call to happen first.
3) __new__ is always called
4) __new__ isn't always called first except in default case.

Do the python syntax checkers check for this?
PyChecker, pylint, PyFlakes
"""

def trace(msg, self_dscr, self, *args, **kwargs):
     print (msg + "\n" 
            + self_dscr + ": " + str(self) 
            + ", args: " + str(args) 
            + ", kwargs: " + str(kwargs))

class Foo(object):
     def __new__(cls, *args, **kwargs):
          trace("in Foo new", "cls", cls, *args, **kwargs) 
          return super(Foo, cls).__new__(cls)
     def __init__(self, *args, **kwargs):
          super(Foo, self).__init__()
          trace("in Foo init", "self", self, *args, **kwargs)

print "Creating Foo object:"
f = Foo()

class Bar(Foo):
    def __init__(self, x):
         print "in Bar init"

print "Creating Bar object:"
b = Bar(1) 

class Baz(Foo):
    def __init__(self, x):
         super(Baz, self).__init__(self, x)
         print "in Baz init"

print "Creating Baz object:"
b = Baz(1) 

class DefaultInit(object):
     def __init__(self):
          print "in DefaultInit"

class Derived(DefaultInit): pass

d = Derived()

class Derived2(DefaultInit):
     def __init__(self):
          print "in Derived2 init"

d2 = Derived2()
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.