Note: I have since figured out inheritance in Python, so this is all just a rant that is no longer relevant, really.
So, I’ve been trying to figure out inheritance in Python. In Lisp (and even C++ and Java), derived objects will have the cons
of their parent called. For example, look at the following C++ snippet:
#include <iostream> using namespace std; class B { public: B() { cout << __PRETTY_FUNCTION__ << endl; } }; class A : public B { public: A() { cout << __PRETTY_FUNCTION__ << endl; } }; int main(int argc, const char **argv) { A *foo = new A(); delete foo; return 0; }
This outputs the following:
B::B() A::A()
As you can tell, the parent object’s constructor is called and then your derived object’s constructor is called. But, in this Python code, the parent object’s constructor is not:
class B: def __init__(self): print "B.__init__(self)" class A(B): def __init__(self): print "A.__init__(self)" A()
This outputs:
A.__init__(self)
Instead you have to do:
class B: def __init__(self): print "B.__init__(self)" class A(B): def __init__(self): B.__init__(self) print "A.__init__(self)" A()
To get the right output:
B.__init__(self) A.__init__(self)
This seems broken to me. But maybe I’m not understanding something. The way I got bit by this was by trying to design a new replacement to debian/rules
which used subclassing. (More on this later.)
Oh, and I intend to try to keep my blog updated more often.