views:

47

answers:

3

Generally don't do OO-programming in Python. This project requires it and am running into a bit of trouble. Here's my scratch code for attempting to figure out where it went wrong:

class trial(object):
    def output( func, x ):
        def ya( self, y ):
            return func( self, x ) + y
        return ya
    def f1( func ):
        return output( func, 1 )
    @f1
    def sum1( self, x ):
        return x

which doesn't compile. I've attempted to add the @staticmethod tag to the "output" and "f1" functions but to no avail. Normally I'd do this

def output( func, x ):
    def ya( y ):
        return func( x ) + y
    return ya

def f1( func ):
    return output( func, 1 )

@f1
def sum1( x ):
    return x

which does work. So how do I get this going in a class?

A: 

No way. This is a wrong design. Follow for The Zen of Python

When you decorate a function calling the decorator by @ it must be already defined.

You must at first - define decorator and at the second step decorate a function.

estin
+3  A: 

There's no need for your method decorators to be a part of the class:

def output(meth, x):
    def ya(self, y):
        return meth(self, x) + y
    return ya

def f1(meth):
    return output(meth, 1)

class trial(object):
    @f1
    def sum1( self, x ):
        return x

>>> trial().sum1(1)
2

I tend to use meth instead of func in decorators I know I'll be applying to methods, just to try to keep it straight in my own head.

Will McCutchen
What if I'm accessing a component of the class? As in instead of `return func(x) + y`, it's `return func(x) + y + self.val`. My example doesn't show that which means I didn't ask a complete and correct question.
wheaties
That will work fine. Notice that the final "replacement" (what's the word for this?) method, `ya`, has access to `self`, which will be the instance on which the method is being called. You can do anything inside of `ya` that you'd do in a normal method on the class.
Will McCutchen
A: 

Try this ugly solution

class trial(object):

    def __init__(self):
        #doing this instead of @ statement
        self.sum1 = self.f1(self.sum1)

    def output(self, func, x ):
        def ya(y):
            return func(x) + y
        return ya

    def f1(self, func):
        return self.output( func, 1 )


    def sum1(self, x ):
        return x

t = trial()

print t.sum1(5)
estin