如何在Python的类方法中访问“静态”类变量?

问题:如何在Python的类方法中访问“静态”类变量?

如果我有以下python代码:

class Foo(object):
    bar = 1

    def bah(self):
        print(bar)

f = Foo()
f.bah()

它抱怨

NameError: global name 'bar' is not defined

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

If I have the following python code:

class Foo(object):
    bar = 1

    def bah(self):
        print(bar)

f = Foo()
f.bah()

It complains

NameError: global name 'bar' is not defined

How can I access class/static variable bar within method bah?


回答 0

代替bar使用self.barFoo.bar。分配给Foo.bar将创建一个静态变量,分配给self.bar将创建一个实例变量。

Instead of bar use self.bar or Foo.bar. Assigning to Foo.bar will create a static variable, and assigning to self.bar will create an instance variable.


回答 1

定义类方法:

class Foo(object):
    bar = 1
    @classmethod
    def bah(cls):    
        print cls.bar

现在,如果bah()必须是实例方法(即可以访问self),则仍可以直接访问类变量。

class Foo(object):
    bar = 1
    def bah(self):    
        print self.bar

Define class method:

class Foo(object):
    bar = 1
    @classmethod
    def bah(cls):    
        print cls.bar

Now if bah() has to be instance method (i.e. have access to self), you can still directly access the class variable.

class Foo(object):
    bar = 1
    def bah(self):    
        print self.bar

回答 2

与所有好的示例一样,您已经简化了实际要执行的操作。这很好,但是值得注意的是,在类变量和实例变量之间,python具有很大的灵活性。方法也可以这样说。有关各种可能性的建议,我建议阅读MichaelFötsch的新型类介绍,尤其是第2至6节。

入门时需要记住很多事情的一件事是python不是java。 不只是陈词滥调。在Java中,整个类都会被编译,使命名空间解析真正简单:方法外(在任何地方)声明的任何变量都是实例(或类,如果是静态的话)变量,并且可以在方法内隐式访问。

使用python的主要经验法则是,按顺序搜索三个命名空间以查找变量:

  1. 功能/方法
  2. 当前模块
  3. 内建

{begin pedagogy}

有一些exceptions。我主要想到的是,在加载类定义时,该类定义是其自己的隐式命名空间。但这仅在模块被加载时才持续,并且在方法内时将被完全忽略。从而:

>>> class A(object):
        foo = 'foo'
        bar = foo


>>> A.foo
'foo'
>>> A.bar
'foo'

但:

>>> class B(object):
        foo = 'foo'
        def get_foo():
            return foo
        bar = get_foo()



Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    class B(object):
  File "<pyshell#11>", line 5, in B
    bar = get_foo()
  File "<pyshell#11>", line 4, in get_foo
    return foo
NameError: global name 'foo' is not defined

{end pedagogy}

最后,要记住的是,您确实有权访问要访问的任何变量,但可能不是隐式的。如果您的目标简单明了,那么选择Foo.bar或self.bar可能就足够了。如果您的示例变得越来越复杂,或者您想做一些继承之类的事情(可以继承静态/类方法!),或者在类本身中引用类名的想法对您来说是错误的,请查看我链接的简介。

As with all good examples, you’ve simplified what you’re actually trying to do. This is good, but it is worth noting that python has a lot of flexibility when it comes to class versus instance variables. The same can be said of methods. For a good list of possibilities, I recommend reading Michael Fötsch’ new-style classes introduction, especially sections 2 through 6.

One thing that takes a lot of work to remember when getting started is that python is not java. More than just a cliche. In java, an entire class is compiled, making the namespace resolution real simple: any variables declared outside a method (anywhere) are instance (or, if static, class) variables and are implicitly accessible within methods.

With python, the grand rule of thumb is that there are three namespaces that are searched, in order, for variables:

  1. The function/method
  2. The current module
  3. Builtins

{begin pedagogy}

There are limited exceptions to this. The main one that occurs to me is that, when a class definition is being loaded, the class definition is its own implicit namespace. But this lasts only as long as the module is being loaded, and is entirely bypassed when within a method. Thus:

>>> class A(object):
        foo = 'foo'
        bar = foo


>>> A.foo
'foo'
>>> A.bar
'foo'

but:

>>> class B(object):
        foo = 'foo'
        def get_foo():
            return foo
        bar = get_foo()



Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    class B(object):
  File "<pyshell#11>", line 5, in B
    bar = get_foo()
  File "<pyshell#11>", line 4, in get_foo
    return foo
NameError: global name 'foo' is not defined

{end pedagogy}

In the end, the thing to remember is that you do have access to any of the variables you want to access, but probably not implicitly. If your goals are simple and straightforward, then going for Foo.bar or self.bar will probably be sufficient. If your example is getting more complicated, or you want to do fancy things like inheritance (you can inherit static/class methods!), or the idea of referring to the name of your class within the class itself seems wrong to you, check out the intro I linked.


回答 3

class Foo(object):
     bar = 1
     def bah(self):
         print Foo.bar

f = Foo() 
f.bah()
class Foo(object):
     bar = 1
     def bah(self):
         print Foo.bar

f = Foo() 
f.bah()

回答 4

class Foo(object):    
    bar = 1

    def bah(object_reference):
        object_reference.var = Foo.bar
        return object_reference.var


f = Foo() 
print 'var=', f.bah()
class Foo(object):    
    bar = 1

    def bah(object_reference):
        object_reference.var = Foo.bar
        return object_reference.var


f = Foo() 
print 'var=', f.bah()