Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
A superclass using a child classes' methods
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Kurt Schwehr  
View profile  
 More options Jun 24, 11:22 am
Newsgroups: comp.lang.python
From: Kurt Schwehr <schw...@gmail.com>
Date: Wed, 24 Jun 2009 08:22:19 -0700 (PDT)
Local: Wed, Jun 24 2009 11:22 am
Subject: A superclass using a child classes' methods
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Torrie  
View profile  
 More options Jun 24, 11:49 am
Newsgroups: comp.lang.python
From: Michael Torrie <torr...@gmail.com>
Date: Wed, 24 Jun 2009 09:49:07 -0600
Local: Wed, Jun 24 2009 11:49 am
Subject: Re: A superclass using a child classes' methods

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"


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jean-Michel Pichavant  
View profile  
 More options Jun 24, 12:23 pm
Newsgroups: comp.lang.python
From: Jean-Michel Pichavant <jeanmic...@sequans.com>
Date: Wed, 24 Jun 2009 18:23:28 +0200
Local: Wed, Jun 24 2009 12:23 pm
Subject: Re: A superclass using a child classes' methods
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kurt Schwehr  
View profile  
 More options Jun 24, 12:37 pm
Newsgroups: comp.lang.python
From: Kurt Schwehr <schw...@gmail.com>
Date: Wed, 24 Jun 2009 09:37:11 -0700 (PDT)
Local: Wed, Jun 24 2009 12:37 pm
Subject: Re: A superclass using a child classes' methods
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]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Craig-Wood  
View profile  
 More options Jun 25, 12:30 pm
Newsgroups: comp.lang.python
From: Nick Craig-Wood <n...@craig-wood.com>
Date: Thu, 25 Jun 2009 11:30:03 -0500
Local: Thurs, Jun 25 2009 12:30 pm
Subject: Re: A superclass using a child classes' methods

Kurt Schwehr <schw...@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 <n...@craig-wood.com> -- http://www.craig-wood.com/nick


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google