小编tec*_*ner的帖子

调用另一个方法时运行Python类方法

我有一个课程如下:

class MyClass(object):
    def __init__(self):
        self.foo = "foo"
        self.bar = "bar"
        self.methodCalls = 0  #tracks number of times any function in the instance is run

    def get_foo(self):
        addMethodCall()
        return self.foo

    def get_bar(self):
        addMethodCall()
        return self.bar

    def addMethodCall(self):
        self.methodCalls += 1
Run Code Online (Sandbox Code Playgroud)

是否有一个内置函数,只要调用方法而不是持续运行,就会调用它addMethodCall()

python class python-2.7

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

标签 统计

class ×1

python ×1

python-2.7 ×1