相关疑难解决方法(0)

Python中的实例变量与类变量

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

类变量:

class MyController(Controller):

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

  def action(self, request):
    pass
Run Code Online (Sandbox Code Playgroud)

实例变量:

class MyController(Controller):

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

  def action(self, request):
    pass
Run Code Online (Sandbox Code Playgroud)

python variables static class member

116
推荐指数
4
解决办法
9万
查看次数

标签 统计

class ×1

member ×1

python ×1

static ×1

variables ×1