Re: inheritance
Hello Ian, Ian Kelly writes: > On Thu, Apr 5, 2012 at 10:50 AM, yag wrote: >> three classes A,B,C and instance x. >> >> now how can I call methods foo in class A and B using 'x' instance. (I hope I >> could pronounce the terminology correct) > > Do you mean that you want C.foo to call B.foo, and B.foo to call > A.foo? If that is the case, just use super(), as you already do with > the __init__ method. > > Or do you want to skip C.foo and call A.foo or B.foo directly? yes! >In that case, just call it from the specific class you want. Since you > are dispatching from the class instead of the instance, I couldn't understand what you mean here, (may be because my poor knowledge with the terminology) >you will have > to pass the instance in explicitly as the self argument. For example: > B.foo(x) # calls B.foo directly with instance x This is interesting, Now I kind of vaguely getting why we keep 'self' argument around in each method. Thanks you -- YYR -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance
Hello Ian, Yagnesh Raghava Yakkala writes: > Hello Ian, > > Ian Kelly writes: [snipped 21 lines] > >>you will have >> to pass the instance in explicitly as the self argument. For example: > >> B.foo(x) # calls B.foo directly with instance x After follow up, I see one problem(can i say that?) with this. With python overwriting methods of super class(es), one need to keep track of all the methods ever defined in the super class(es). do everybody do a check before writing a new method for existing method with the same name.? -- YYR -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance
Hello Chris, Chris Angelico writes: > > If you're subclassing something, you should generally work with the > intention that an instance of your class will function viably in any > situation in which an instance of the parent is wanted. So if you're > writing a method of the same name as one in the parent, you should be > performing the exact same task and with the same parameters (possibly > having more parameters accepted, but default values set for them). > That's part of what it means to subclass. Thanks for explaining. It makes sense. I see that python interpreter can instantly tell the list of defined methods in the super class(es). So "keeping track" thing is not at all a problem. Thanks a lot again. -- YYR -- http://mail.python.org/mailman/listinfo/python-list
[solved] Re: inheritance
Hello, With Chris and Ian help (Thank you both) I end up writing the example script like this,(just for the record) - #! /usr/bin/env python3 # -*- coding: utf-8 -*- class A(object): def __init__(self, a): print('a = ', a) self.a = a def foo(self): print('printing from foo in A = ',self.a) class B(A): def __init__(self, b, a): super(B, self).__init__(a) print('b = ', b) self.b = b def foo(self): print('printing from foo in B = ',self.b) def bar(self): print('printing from foo in B = ',self.b) class C(B): def __init__(self, c, b, a): super(C, self).__init__(b, a) print('c = ', c) self.c = c def foo(self): print('printing from foo in C = ',self.c) def baz(self): print('printing from foo in C = ',self.c) x = C(3,2,1) x.bar() x.baz() print("") x.foo() B.foo(x) A.foo(x) # inheritance.py ends here - -- YYR -- http://mail.python.org/mailman/listinfo/python-list