Invoke a superclass method from a subclass constructor

Ian Kelly ian.g.kelly at gmail.com
Tue Sep 13 19:24:34 CEST 2011


On Tue, Sep 13, 2011 at 10:56 AM, Kayode Odeyemi <dreyemi at gmail.com> wrote:
>>>> class B(A):
> ...     def __init__(self, module):
> ...             self.module = A.log(self, module)
> ...             print self.module # printing here is completely unnecessary
> in a good OOP language
> ...
>>>> c = B('system')
> logged
>>>> class B(A):
> ...     def __init__(self, module):
> ...             print super(B, self).log('system') # printing here is
> completely unnecessary in a good OOP language
> ...
>>>> c = B('system')
> logged
>>>>
> When an instance of a class is created, all codes within that instance block
> should be executed. That's my understanding of OOP.

The initializer should be executed, which is what Python does.  Your
initializer then calls A.log, which does nothing interesting at all.

My question is, what exactly is it that you intend A.log to do?  As
written, it does not do any logging.  It merely constructs a string
and then returns it.  Neither constructing a string, nor returning a
string, imply logging it or printing it.



More information about the Python-list mailing list