相关疑难解决方法(0)

在Python中,如何在类方法中访问"静态"类变量

如果我有以下python代码:

class Foo(object):
    bar = 1

    def bah(self):
        print(bar)

f = Foo()
f.bah()
Run Code Online (Sandbox Code Playgroud)

它抱怨

NameError: global name 'bar' is not defined
Run Code Online (Sandbox Code Playgroud)

如何bar在方法中访问类/静态变量bah

python

146
推荐指数
4
解决办法
14万
查看次数

标签 统计

python ×1