小编Yan*_*dad的帖子

如何将功能移到课堂?蟒蛇

我想在classmethod中使用服务函数,其中服务函数在其他地方定义.我希望是动态的,所以我可以在不同的情况下定义不同的功能.我试过这个:

def print_a():
    print 'a'

class A:
    func = print_a
    @classmethod
    def apply(cls):
        cls.func()

A.apply()
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

unbound method print_a() must be called with A instance as first argument (got nothing instead)

任何想法如何使其工作?

python class-attributes

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

标签 统计

class-attributes ×1

python ×1