将模块方法分配给Class变量或Instance变量

卡尔兹

在模块a.py中

def task(): 
    print "task called"

a = task

class A:

    func = task              # this show error unbound method
    #func = task.__call__    # if i replace with this work

    def __init__(self):
        self.func_1 = task

    def test_1(self):
        self.func_1()

    @classmethod
    def test(cls):
        cls.func()


a()
A().test_1()
A.test()

输出:

task called
task called
Traceback (most recent call last):
  File "a.py", line 26, in <module>
     A.test()
  File "a.py", line 21, in test
     cls.func()
TypeError: unbound method task() must be called with A instance as 
first argument (got nothing instead)

在模块中,我可以轻松地将函数分配给变量。当在类内部尝试将模块级功能分配给类变量func = task时,它显示错误,要消除此错误,我必须将其替换为func = task .__ call__,但是当我将其工作分配给实例变量self.func_1 = task时

我的问题是:为什么没有__call__不能将模块级别的函数分配给类变量,并且可以分配给实例变量的相同函数正在起作用。

香气

由于您将函数映射为的未绑定方法A,因此在调用时cls.func,首先要问等于getattr(cls, 'func')返回<unbound method A.task>BUT的对象,因此需要使用class作为第一个参数来调用此未绑定方法。

所以,因为在这种特殊情况下cls.func的意思是“给我类属性funccls”它不能手段,同时“调用类的方法func” -那么Python没有翻译cls.func()func(cls)

但是在同一时间,因为func<unbound method A.task>(绑定到A.task),因此需要像func(cls)工作一样被调用

用类似的方法检查它:

@classmethod
def test(cls):
    print getattr(cls, 'func') # <unbound method A.task>

您可以使用以下方法修复它:

def task(cls=None):
    if cls is None:
        print 'task()'
    else:
        print 'A.foo({})'.format(cls)

a = task

class A:
    func = task             # this show error unbound method

    def __init__(self):
        self.func_1 = task

    def test_1(self):
        self.func_1()

    @classmethod
    def test(cls):
        cls.func(cls())

a()
A().test_1()
A.test()

输出:

task()
task()
A.foo(<__main__.A instance at 0x7fd0310a46c8>)

请注意,python3删除了未绑定的方法,这仅适用于python2.x

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章