<div class="gmail_quote">On Sun, Sep 11, 2011 at 11:41 AM, Thomas Jollans <span dir="ltr"><<a href="mailto:t@jollybox.de">t@jollybox.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div><div></div><div class="h5">On 11/09/11 10:18, Kayode Odeyemi wrote:<br>
> Hello friends,<br>
><br>
> An instance of my subclass doesn't invoke its superclass method, except<br>
> when it is referenced<br>
> directly.<br>
><br>
> Here is what I mean:<br>
><br>
>>>> class A(object):<br>
> ...     def log(self, module):<br>
> ...             return str('logged')<br>
> ...<br>
><br>
>>>> class B(A):<br>
> ...     def __init__(self, module):<br>
> ...             self.module = A().log(module)<br>
> ...<br>
>>>> c = B('system')<br>
>>>> # I expect 'logged' to be printed here<br>
>>>> print c.log('system') # why do I have to do this?<br>
>>>> 'logged'<br>
><br>
> Why do I have to make a call to c.log before log() method can be invoked?<br>
><br>
> My reasoning is such that since I have passed the log() method to B's<br>
> constructor, an instance<br>
> of B should invoke A's log() method.<br>
><br>
> What could I be missing in class A or B to have this working as expected?<br>
<br>
</div></div>It is working:<br>
<div class="im"><br>
>>> class A(object):<br>
...     def log (self, module):<br>
...         return str ('logged')<br>
...<br>
>>> class B(A):<br>
...     def __init__(self, module):<br>
...         self.module = A().log(module)<br>
...<br>
>>> c = B('system')<br>
</div>>>> c.module<br>
'logged'<br>
<br></blockquote><div>Why do you have to do c.module? I'm expecting an output just by creating an instance of B.</div><div>Could this be a limitation in Py2+?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">


In B's constructor, you create a new instance of A (a pointless<br>
excersise - you already have an instance of A, "self"), call its log()<br>
method, and store the return value of that method in the attribute "module".<br>
<br>
This is how you'd usually call a superclass's method:<br>
<br>
>>> class B2(A):<br>
...     def __init__(self, module):<br>
...         # This is the way to call a superclass method:<br>
...         self.log (module)<br>
...<br>
>>> c2 = B2 ('system')<br>
>>><br>
<br>
That's what inheritance is. If B is derived from A, any B *is* also an A.<br>
<br>
Now, something that might be closer to what you're actually heading for,<br>
to think about. Please note that I'm using Python 3: explicitly deriving<br>
a class from object is no longer necessary, and the "super" function is<br>
now simpler. If you want to use Python 2, I'll leave looking up how to<br>
use its "super" function as an excersise to the reader.<br>
<br>
>>> class ModuleLogger:<br>
...     def __init__(self, module):<br>
...         self.module = module<br>
...         print ('module logged: {0}'.format (module))<br>
...<br>
>>> class Foo (ModuleLogger):<br>
...     def __init__(self, module):<br>
...         # calling a superclass constructor here, in Python 3 syntax:<br>
...         super().__init__(module)<br>
...<br>
>>> f = Foo ('system')<br>
module logged: system<br>
>>><br>
<br></blockquote><div>I'm on Py2.7.</div><div><br></div><div>class ModuleLogger is not the same as class A (the one I'm using as an example). I need the log()</div><div>method to be an instance of class A. I don't want it declared in class A constructor because I don't</div>

<div>want to have access to it when class A is instantiated.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Using super() is always necessary when calling a superclass' method that<br>
is also implemented in the subclass - self.__init__ would just invoke<br>
the Foo constructor, you need super() to get to the ModuleLogger<br>
constructor.<br></blockquote><div><br></div><div>Well, I did try using super(), but I got this: </div><div><div>>>> class B(A):</div><div>...     def __init__(self, module):</div><div>...             super(A, self).log('system')</div>

<div>...</div><div>>>> c = B('module')</div><div>Traceback (most recent call last):</div><div>  File "<stdin>", line 1, in <module></div><div>  File "<stdin>", line 3, in __init__</div>

<div>AttributeError: 'super' object has no attribute 'log'</div></div><div><br></div><div>I hope a reader on Py2+ can help with this.</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">


<br>
 - Thomas<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><br clear="all"><div><br></div>-- <br>Odeyemi 'Kayode O.<br><a href="http://www.sinati.com" target="_blank">http://www.sinati.com</a>. t: @charyorde<br><br>