As Python is a dynamic language, there are a lot you can play with. However, it also produces a lot of confusion. As one of those, inheriting (or extending) of classes is sometimes different from expected. The following snippet shows a case.
>>> class A(): ... def __init__(self, arg): ... self.arg = arg ... self.afunc = None >>> class B(A): ... def __init__(self, arg): ... A.__init__(self, arg) ... def afunc(): ... print 'haha' >>> b = B(5) >>> b.afunc() Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: 'NoneType' object is not callable
The purpose was to extend the the original class meanwhile fixing the implementation of afunc()
. However, it fails. The function definition of afunc() is overrode by the assigning statement in A.__init__()
. Thus the sequence of name binding is like BaseClass
-> Subclass
-> Base.__init__()
-> Subclass.__init__()
.
Posted in: Python
Posted on June 4, 2013
0