I often find myself overwriting methods of a parent class, and can never decide if I should explicitly list given parameters or just use a blanket *args, **kwargs
construct. Is one version better than the other? Is there a best practice? What (dis-)advantages am I missing?
class Parent(object):
def save(self, commit=True):
# ...
class Explicit(Parent):
def save(self, commit=True):
super(Explicit, self).save(commit=commit)
# more logic
class Blanket(Parent):
def save(self, *args, **kwargs):
super(Blanket, self).save(*args, **kwargs)
# more logic
Perceived benefits of explicit variant
- More explicit (Zen of Python)
- easier to grasp
- function parameters easily accessed
Perceived benefits of blanket variant
- more DRY
- parent class is easily interchangeable
- change of default values in parent method is propagated without touching other code
**kwargs
. If this isn't likely, then explicitly specifying kwargs is definitely much better from a readability standpoint. – Touchlinesuper
and multiple inheritance), or 2) Use**kwargs
and allow changing the signature of the methods. Which one should be used probably depends on the situation. – Respective