Reputation: 2418
In the below code I have to refer to Foo
a second time (i.e. in super(Foo, self)
Is there anything smarter I can insert, such that if I rename Foo, the other smarter tag doesnt need to be updated? It does not seem like very DRY code.
Note: I have to specify the starting point for super, the arg can't just be left out because this class gets extended.
class Foo(Bar):
def __init__(self, *args, **kwargs):
super(Foo, self).__init__(*args, **kwargs)
Upvotes: 0
Views: 59
Reputation: 8035
super()
without arguments in Python 3 is the equivalent of super(Foo, self)
in your case, and based on your bold description it is also expected to work- even if Foo
is a parent in subsequent code.
class Foo(Bar):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Should work fine. There are three ways of using super()
:
class Parent:
def __init__(self, a):
self.a = a
class Child(Parent):
def __init__(self, a, b):
super(Child, self).__init__(a)
self.b = b
class ChildSuper(Parent):
def __init__(self, a, b):
super().__init__(a)
self.b = b
class ChildClass(Parent):
def __init__(self, a, b):
Parent.__init__(self, a)
self.b = b
assert Child(1, 2).a == 1
assert ChildSuper(1, 2).a == 1
assert ChildClass(1, 2).a == 1
Upvotes: 1