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.
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"
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.
> 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.
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.
> 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.
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.