小编bri*_*ian的帖子

在Python中调用另一个类方法

我想要创建一个包含对另一个类方法的引用的类.我希望能够调用该方法.它基本上是一种做回调的方法.

我的代码一直有效,直到我尝试访问类var.当我运行下面的代码时,我得到错误我做错了什么?

布赖恩

import logging

class yRunMethod(object):
    """
    container that allows method to be called when method run is called 
    """

    def __init__(self, method, *args):
        """
        init
        """

        self.logger = logging.getLogger('yRunMethod')
        self.logger.debug('method <%s> and args <%s>'%(method, args))

        self.method = method
        self.args   = args

    def run(self):
    """
    runs the method
    """

        self.logger.debug('running with <%s> and <%s>'%(self.method,self.args))

        #if have args sent to function
        if self.args:
            self.method.im_func(self.method, *self.args)

        else:
            self.method.im_func(self.method)

if __name__ == "__main__":  
    import sys

    #create test class
    class testClass(object):
        """
        test …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
3489
查看次数

标签 统计

python ×1