我有以下可调用的:
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方法)都不会调用加法函数。
所需的功能是SomeFunction ( some_method)调用时应该调用加法)。
发布于 2018-03-25 13:25:43
假设expr将是一个方法/函数,并且假设您知道什么方法/函数返回,那么您有3个选项。只要遵循这3种选择中的一种,你就能实现你想要的。
1)您可以在分配给expr的self.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 here2)如果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 example3)在实例化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类的方法/函数,否则将永远无法检查该方法/函数的结果。希望一切都很清楚。
发布于 2018-03-25 12:42:54
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')https://stackoverflow.com/questions/49475889
复制相似问题