A superclass using a child classes' methods

K

Kurt Schwehr

I'm trying to build an OO system for encoding and decoding
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,
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
 
M

Michael Torrie

Kurt said:
I'm trying to build an OO system for encoding and decoding
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,
where the alphabet attribute is set in the child, but used in the
parent.

There is no difference between an attribute and a method. They are both
attributes. One happens to be callable and is a method call.

I just tried it and it seemed to work fine. In my old C++ days, I
believe you would call this scenario virtual classes. In python, since
everything is dynamic and computed at runtime, I think you can just
refer to any attribute in the instance and as long as someone down the
road provides it, it would work.

For example:

class parent(object):
def test(self):
print self.child_attribute
self.child_method()

class child(parent):
def __init__(self):
self.child_attribute = 'child'

def child_method(self):
print 'child method is called'


c=child()
c.test()

This should display "child" and "child method is called"
 
J

Jean-Michel Pichavant

Kurt said:
I'm trying to build an OO system for encoding and decoding
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.
hell no, this works for methods as well.
Is that right? For attributes I found this example,
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
class Parent:
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
 
K

Kurt Schwehr

Jean-Michel,

Thanks for the excellent response setting me straight. Now to figure
out what dumb thing I did to make my code not work...

-kurt


[clear example of it working]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top