将方法参数传递给类

时间:2018-03-25 12:33:47

标签: python class oop

我有以下可调用:

class SomeClass(object):

    def __init__(self, expr, return_status):

        self._expr = expr
        self._status = return_status


    def __call__(self):   

        if self._expr == self._status:
            return True

    def __str__(self):

        return ("Function: %s\n     Return Status: %s\n" %
                (self.__class__.__name__, self._status))

我面临的问题是,每当我尝试传递一个像:

这样的表达式时
some_variable = SomeFunction(SomeClass.some_method,return_status=True)

SomeClass.some_method被评估并作为布尔值存储在self._expr中。

我真正想要的是将此表达式(SomeClass.some_method)存储在self._expr中,并在每次调用__call__(self)方法时进行评估。

我有道理吗?

假设我采用以下示例:

def addition(c,b):
        print "'addition' function called!\n"
        sum = c+b
        if sum>5:
            return True
        else:
            return False

    script_timeout = 3


    some_variable = SomeFunction(addition(1,2),return_status=True)

    print "some_variable: \n%s" %some_variable

    some_class.some_method(some_variable, script_timeout, 1)

这给了我以下输出:

'addition' function called!

SomeFunction (_init_) function called!

expr: False

self._expr = False and self._status = True

SomeFunction (_str_) function called!

self.__class__.__name__ = SomeFunction and self._expr = False

monitor: 
Function: SomeFunction
     Return Status of the Expression: True

SomeFunction (_call_) function called!

self._expr = False and self._status = True

SomeFunction (_call_) function called!

self._expr = False and self._status = True

SomeFunction (_call_) function called!

self._expr = False and self._status = True

因此,关注的是添加函数在每次迭代调用 SomeFunction 时都没有被调用(通过 some_method 方法。)< / p>

所需的功能是 SomeFunction (当由 some_method 调用时)应调用添加功能。

2 个答案:

答案 0 :(得分:1)

假设expr将是一个方法/函数并假设您知道返回什么方法/函数,那么您有3个选项。只需遵循这3个选项中的一个,即可实现您的目标。

1)您可以在expr的分配中致电self.expr

....
class CheckStatus:
    def __init__(self, expr, ...)
        self.expr = expr() # expr() being called here, store whatever the result is to self.expr

    def __call__(self):
        # self.expr already holds a boolean value, or anything your expr returns
        if self.expr == self.status:
            # self.expr match ...
            # do what else you have to do

obj = CheckStatus(SomeClass().method, ...) # pay attention how we pass the method here

2)如果self.expr是对expr的引用,那么:

class CheckStatus:
    def __init__(self, expr, ...):
        self.expr = expr

    def __call__(self):
        # in this example, self.expr now it's a reference to some method
        # thus you need to call self.expr here
        if self.expr() == self.status:
            ....

obj = CheckStatus(SomeClass().method, ...) # pay attention how we pass method for this example

3)在实例化SomeClass().method()

时调用CheckStatus()
class CheckStatus:
    def __init__(self, expr, ...):
        self.expr = expr # for this example, expr already is a boolean or anything else some method/function returned

    def __call__(self):
        # don't need to call anything here
        if self.expr == self.status:
            ....

obj = CheckStatus(SomeClass().method(), ...) # because here we called SomeClass().method()

你必须调用你在某个地方传入CheckStatus类的方法/函数,否则你将永远不会检查那个方法/函数结果。 希望很清楚。

答案 1 :(得分:-2)

class S(object):
    def __init__(self, expr, return_status):
        self._expr = expr
        self._status = return_status

    def __call__(self):   
        if self._expr() == self._status:
            raise Exception
        self._expr()

    def __str__(self):
        return ("Function: %s\n     Return Status of the Expression: %s\n" %
                (self.__class__.__name__, self._status))

def some_method():return True
try:
    S(some_method,return_status=True)()
except Exception as e:
    print('Got Exception')