Re: A superclass using a child classes' methods
- From: Jean-Michel Pichavant <jeanmichel@xxxxxxxxxxx>
- Date: Wed, 24 Jun 2009 18:23:28 +0200
Kurt Schwehr wrote:
I'm trying to build an OO system for encoding and decodinghell no, this works for methods as well.
datapackets. I'd like the parent class to have an encode function
that uses each of the child classes' packing methods. It appears that
this works for attributes of children accessed by the parent, but not
for methods.
Is that right? For attributes I found this example,class Parent:
where the alphabet attribute is set in the child, but used in the
parent.
http://www.pasteur.fr/formation/infobio/python/ch19s04.html
Should I just set an attribute in the child class and then call the
super's functionality making is pull the data from the attribute?
Thanks,
-kurt
def foo(self):
raise NotImplementedError() # virtual method, it has to be overriden by childs
def bar(self):
self.foo()
class Son(Parent):
def foo(self):
print "I'm your son"
class Daughter(Parent):
def foo(self):
print "I'm your daughter"
Son().bar()
"I'm your son"
Declaring the foo method at the Parent level is not required. But it's a good practice: explicit > implicit and it helps to write proper documentation.
Jean-Michel
.
- Follow-Ups:
- Re: A superclass using a child classes' methods
- From: Kurt Schwehr
- Re: A superclass using a child classes' methods
- References:
- A superclass using a child classes' methods
- From: Kurt Schwehr
- A superclass using a child classes' methods
- Prev by Date: Unable to get Tkinter menubar to appear under OS X
- Next by Date: Re: Get name of class without instance
- Previous by thread: Re: A superclass using a child classes' methods
- Next by thread: Re: A superclass using a child classes' methods
- Index(es):
Relevant Pages
|