Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A superclass using a child classes' methods

4,549 views
Skip to first unread message

Kurt Schwehr

unread,
Jun 24, 2009, 11:22:19 AM6/24/09
to
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

Michael Torrie

unread,
Jun 24, 2009, 11:49:07 AM6/24/09
to pytho...@python.org
Kurt Schwehr wrote:
> 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"


Jean-Michel Pichavant

unread,
Jun 24, 2009, 12:23:28 PM6/24/09
to Kurt Schwehr, pytho...@python.org
Kurt Schwehr wrote:
> 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

Kurt Schwehr

unread,
Jun 24, 2009, 12:37:11 PM6/24/09
to
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

On Jun 24, 12:23 pm, Jean-Michel Pichavant <jeanmic...@sequans.com>
wrote:


> Kurt Schwehr wrote:
> > 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.
>

[clear example of it working]

Nick Craig-Wood

unread,
Jun 25, 2009, 12:30:03 PM6/25/09
to
Kurt Schwehr <sch...@gmail.com> wrote:
> Thanks for the excellent response setting me straight. Now to figure
> out what dumb thing I did to make my code not work...

If you have trouble then post the code (or a cut down version
demonstrating the code) - it always helps to have real code with real
error messages when tracking down problems. A lot of people (like me)
will enjoy the puzzle of looking through your code and finding out
where it went wrong.

--
Nick Craig-Wood <ni...@craig-wood.com> -- http://www.craig-wood.com/nick

0 new messages