Discussion:
[PEAK] Inheritance and @when from PEAK-rules
Athanasios Anastasiou
2012-07-16 18:02:06 UTC
Permalink
Hello everyone

I have been using PEAK-rules with functions but i came across some
strange behaviour when it came to using it with classes and inherited
methods.

The problem (in one line) is that i find it difficult to get @when to
call the "right" (overriden) function.

I wish to define an abstract class with a number of calls depending on
the type of the parameters once and then modify the functionality of
derived handlers later depending on what each handler is supposed to be
doing.

What i am doing at the time is roughly this:

class someAbstractClass(object):
def theFunPrototype(self,p1,p2,p3):
print "Abstract call - unhandled %s %s %s" % (type(p1),type(p2),type(p3))

@when(theFunPrototype, (int,int,int))
def _theFunProtoForInts(self,p1,p2,p3):
print "All three parameters are integers"

@when(theFunPrototype, (int,int,str))
def _theFunProtoForIntStr(self,p1,p2,p3):
print "Two integers and one string"

class someConcreteClass(someAbstractClass):
def _theFunProtoForInts(self,p1,p2,p3):
print "Overriden handler %s %s %s" % (p1,p2,p3)


someConcreteClass().theFunPrototype(5,6,7) will execute the
someAbstractClass' method. If i wanted this thing to work as expected, i
would have to override a "dummy" theFunPrototype which would call its
corresponding "super" and add a @when on the derived
"_theFunProtoForInts" but this seems a bit like duplicating the exact
same thing on the derived class without any reason...Since both classes
share a _theFunPrototypeForInts, the right behaviour would be to call
the derived class....(or maybe not? :-) ).

Any ideas about where i could be going wrong?

Looking forward to hearing from you
Athanasios Anastasiou
PJ Eby
2012-07-16 19:07:19 UTC
Permalink
On Mon, Jul 16, 2012 at 12:56 PM, Athanasios Anastasiou
Post by Athanasios Anastasiou
Hello everyone
I have been using PEAK-rules with functions but i came across some strange
behaviour when it came to using it with classes and inherited methods.
the "right" (overriden) function.
If you want to use generic functions as methods, you must use
@when(BaseClass.methodName.im_func) when defining overrides in a
subclass. (Otherwise, your new method either will be called in place
of the generic function (if named the same) or it will not be called
at all (if it has a different name).

And, if you want that method to be able to call previous methods, use
the next_method machinery, not super(). (Otherwise, you will simply
call the same generic function a second time!)
Athanasios Anastasiou
2012-07-17 10:40:29 UTC
Permalink
Dear Phillipe

Thank you very much for your help, i vaguely remember trying just
BaseClass.methodName before but don't remember why i did not keep it.

Anyway, BaseClass.methodName.im_func works fine.

All the best
Athanasios Anastasiou
Post by PJ Eby
On Mon, Jul 16, 2012 at 12:56 PM, Athanasios Anastasiou
Post by Athanasios Anastasiou
Hello everyone
I have been using PEAK-rules with functions but i came across some strange
behaviour when it came to using it with classes and inherited methods.
the "right" (overriden) function.
If you want to use generic functions as methods, you must use
@when(BaseClass.methodName.im_func) when defining overrides in a
subclass. (Otherwise, your new method either will be called in place
of the generic function (if named the same) or it will not be called
at all (if it has a different name).
And, if you want that method to be able to call previous methods, use
the next_method machinery, not super(). (Otherwise, you will simply
call the same generic function a second time!)
Loading...