标签归档:member

Python中的实例变量与类变量

问题:Python中的实例变量与类变量

我有Python类,在运行时我只需要一个实例,因此每个类仅一个属性,而每个实例仅具有一个属性就足够了。如果将有多个实例(不会发生),则所有实例都应具有相同的配置。我不知道以下哪个选项会更好或更“惯用” Python。

类变量:

class MyController(Controller):

  path = "something/"
  children = [AController, BController]

  def action(self, request):
    pass

实例变量:

class MyController(Controller):

  def __init__(self):
    self.path = "something/"
    self.children = [AController, BController]

  def action(self, request):
    pass

I have Python classes, of which I need only one instance at runtime, so it would be sufficient to have the attributes only once per class and not per instance. If there would be more than one instance (which won’t happen), all instance should have the same configuration. I wonder which of the following options would be better or more “idiomatic” Python.

Class variables:

class MyController(Controller):

  path = "something/"
  children = [AController, BController]

  def action(self, request):
    pass

Instance variables:

class MyController(Controller):

  def __init__(self):
    self.path = "something/"
    self.children = [AController, BController]

  def action(self, request):
    pass

回答 0

如果您仍然只有一个实例,那么最好每个实例都设置所有变量,这仅仅是因为它们的访问速度(稍微快一点)(由于类与实例之间的“继承性”,因此“查找”的级别降低了),而且没有不利的一面来抵消这一小优势。

If you have only one instance anyway, it’s best to make all variables per-instance, simply because they will be accessed (a little bit) faster (one less level of “lookup” due to the “inheritance” from class to instance), and there are no downsides to weigh against this small advantage.


回答 1

进一步呼应MikeAlex的建议,并添加我自己的颜色…

使用实例属性是典型的……更加惯用的Python。由于类属性的用例是特定的,因此未使用过多的类属性。静态方法和类方法与“普通”方法一样。它们是解决特定用例的特殊结构,否则它是由异常的程序员创建的代码,目的是炫耀他们知道Python编程的一些晦涩之处。

Alex在他的答复中提到,由于查找级别降低了,访问将(稍微快一些)……让我进一步澄清那些还不知道如何工作的人。它与变量访问非常相似-搜索顺序为:

  1. 当地人
  2. 非本地人
  3. 全球
  4. 内建

对于属性访问,顺序为:

  1. 实例
  2. MRO确定的基本类(方法解析顺序)

两种技术都以“由内而外”的方式工作,这意味着首先检查大多数局部对象,然后依次检查外层。

在上面的示例中,假设您正在查找path属性。当遇到“ self.path”之类的引用时,Python将首先查看实例属性以进行匹配。如果失败,它将检查实例化对象的类。最后,它将搜索基类。如Alex所述,如果在实例中找到您的属性,则无需在其他地方查找,因此节省了一点时间。

但是,如果您坚持使用类属性,则需要进行额外的查找。或者,您的另一种选择是通过类而不是实例来引用对象,例如,MyController.path代替self.path。这是一个直接查找,可以绕开延迟查找,但是正如alex在下面提到的那样,它是一个全局变量,因此您丢失了原本想保存的那一部分(除非您创建对[global]类名的本地引用) )。

最重要的是,您应该在大多数时间使用实例属性。但是,在某些情况下,类属性是适合该工作的工具。同时使用这两个代码将需要最大的努力,因为使用self只会使您获得实例属性对象,并且可以通过影子访问相同名称的class属性。在这种情况下,必须使用通过类名访问属性,以便对其进行引用。

Further echoing Mike’s and Alex’s advice and adding my own color…

Using instance attributes are the typical… the more idiomatic Python. Class attributes are not used used as much, since their use cases are specific. The same is true for static and class methods vs. “normal” methods. They’re special constructs addressing specific use cases, else it’s code created by an aberrant programmer wanting to show off they know some obscure corner of Python programming.

Alex mentions in his reply that access will be (a little bit) faster due to one less level of lookup… let me further clarify for those who don’t know about how this works yet. It is very similar to variable access — the search order of which is:

  1. locals
  2. nonlocals
  3. globals
  4. built-ins

For attribute access, the order is:

  1. instance
  2. class
  3. base classes as determined by the MRO (method resolution order)

Both techniques work in an “inside-out” manner, meaning the most local objects are checked first, then outer layers are checked in succession.

In your example above, let’s say you’re looking up the path attribute. When it encounters a reference like “self.path“, Python will look at the instance attributes first for a match. When that fails, it checks the class from which the object was instantiated from. Finally, it will search the base classes. As Alex stated, if your attribute is found in the instance, it doesn’t need to look elsewhere, hence your little bit of time savings.

However, if you insist on class attributes, you need that extra lookup. Or, your other alternative is to refer to the object via the class instead of the instance, e.g., MyController.path instead of self.path. That’s a direct lookup which will get around the deferred lookup, but as alex mentions below, it’s a global variable, so you lose that bit that you thought you were going to save (unless you create a local reference to the [global] class name).

The bottom-line is that you should use instance attributes most of the time. However, there will be occasions where a class attribute is the right tool for the job. Code using both at the same time will require the most diligence, because using self will only get you the instance attribute object and shadows access to the class attribute of the same name. In this case, you must use access the attribute by the class name in order to reference it.


回答 2

如有疑问,您可能需要实例属性。

类属性最好保留给有意义的特殊情况。唯一非常常见的用例是方法。对实例需要知道的只读常量使用类属性并不罕见(尽管这样做的唯一好处是,如果您还希望从类外部进行访问),但是对于在其中存储任何状态,您一定要谨慎,这很少是您想要的。即使您只有一个实例,也应该像编写其他实例一样编写类,这通常意味着使用实例属性。

When in doubt, you probably want an instance attribute.

Class attributes are best reserved for special cases where they make sense. The only very-common use case is methods. It isn’t uncommon to use class attributes for read-only constants that instances need to know (though the only benefit to this is if you also want access from outside the class), but you should certainly be cautious about storing any state in them, which is seldom what you want. Even if you will only have one instance, you should write the class like you would any other, which usually means using instance attributes.


回答 3

关于在Python中访问类变量的性能存在相同的问题-此处的代码改编自@Edward Loper

局部变量是访问最快的,与模块变量,类变量,实例变量密切相关。

您可以从以下四个范围访问变量:

  1. 实例变量(self.varname)
  2. 类变量(Classname.varname)
  3. 模块变量(VARNAME)
  4. 局部变量(变量名)

考试:

import timeit

setup='''
XGLOBAL= 5
class A:
    xclass = 5
    def __init__(self):
        self.xinstance = 5
    def f1(self):
        xlocal = 5
        x = self.xinstance
    def f2(self):
        xlocal = 5
        x = A.xclass
    def f3(self):
        xlocal = 5
        x = XGLOBAL
    def f4(self):
        xlocal = 5
        x = xlocal
a = A()
'''
print('access via instance variable: %.3f' % timeit.timeit('a.f1()', setup=setup, number=300000000) )
print('access via class variable: %.3f' % timeit.timeit('a.f2()', setup=setup, number=300000000) )
print('access via module variable: %.3f' % timeit.timeit('a.f3()', setup=setup, number=300000000) )
print('access via local variable: %.3f' % timeit.timeit('a.f4()', setup=setup, number=300000000) )

结果:

access via instance variable: 93.456
access via class variable: 82.169
access via module variable: 72.634
access via local variable: 72.199

Same question at Performance of accessing class variables in Python – the code here adapted from @Edward Loper

Local Variables are the fastest to access, pretty much tied with Module Variables, followed by Class Variables, followed by Instance Variables.

There are 4 scopes you can access variables from:

  1. Instance Variables (self.varname)
  2. Class Variables (Classname.varname)
  3. Module Variables (VARNAME)
  4. Local Variables (varname)

The test:

import timeit

setup='''
XGLOBAL= 5
class A:
    xclass = 5
    def __init__(self):
        self.xinstance = 5
    def f1(self):
        xlocal = 5
        x = self.xinstance
    def f2(self):
        xlocal = 5
        x = A.xclass
    def f3(self):
        xlocal = 5
        x = XGLOBAL
    def f4(self):
        xlocal = 5
        x = xlocal
a = A()
'''
print('access via instance variable: %.3f' % timeit.timeit('a.f1()', setup=setup, number=300000000) )
print('access via class variable: %.3f' % timeit.timeit('a.f2()', setup=setup, number=300000000) )
print('access via module variable: %.3f' % timeit.timeit('a.f3()', setup=setup, number=300000000) )
print('access via local variable: %.3f' % timeit.timeit('a.f4()', setup=setup, number=300000000) )

The result:

access via instance variable: 93.456
access via class variable: 82.169
access via module variable: 72.634
access via local variable: 72.199