首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将方法参数传递给类

将方法参数传递给类
EN

Stack Overflow用户
提问于 2018-03-25 12:33:46
回答 2查看 75关注 0票数 0

我有以下可调用的:

代码语言:javascript
运行
复制
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))

我面临的问题是,每当我试图传递这样的表达式时:

代码语言:javascript
运行
复制
some_variable = SomeFunction(SomeClass.some_method,return_status=True)

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

实际上,我希望这个表达式(SomeClass.some_method)存储在self._expr中,并在每次调用__call__(self)方法时得到评估。

我说得通吗?

假设我以下面的例子为例:

代码语言:javascript
运行
复制
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)

这给了我以下输出:

代码语言:javascript
运行
复制
'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)调用时应该调用加法)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-25 13:25:43

假设expr将是一个方法/函数,并且假设您知道什么方法/函数返回,那么您有3个选项。只要遵循这3种选择中的一种,你就能实现你想要的。

1)您可以在分配给exprself.expr中调用self.expr

代码语言:javascript
运行
复制
....
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的引用,那么:

代码语言:javascript
运行
复制
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()

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2018-03-25 12:42:54

代码语言:javascript
运行
复制
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')
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49475889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档