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