相关疑难解决方法(0)

"超级"在Python中做了什么?

有什么区别:

class Child(SomeBaseClass):
    def __init__(self):
        super(Child, self).__init__()
Run Code Online (Sandbox Code Playgroud)

和:

class Child(SomeBaseClass):
    def __init__(self):
        SomeBaseClass.__init__(self)
Run Code Online (Sandbox Code Playgroud)

我已经看到super在只有单继承的类中使用了很多.我可以看到为什么你在多重继承中使用它,但不清楚在这种情况下使用它的优点是什么.

python oop inheritance super

522
推荐指数
7
解决办法
21万
查看次数

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万
查看次数

标签 统计

python ×2

class ×1

inheritance ×1

member ×1

oop ×1

static ×1

super ×1

variables ×1