Inheritance best practice : *args, **kwargs or explicitly specifying parameters

后端 未结 6 1633
长发绾君心
长发绾君心 2020-12-23 02:10

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

6条回答
  •  暖寄归人
    2020-12-23 02:27

    Not really an answer but more a side note: If you really, really want to make sure the default values for the parent class are propagated to the child classes you can do something like:

    class Parent(object):
    
        default_save_commit=True
        def save(self, commit=default_save_commit):
            # ...
    
    class Derived(Parent):
    
        def save(self, commit=Parent.default_save_commit):
            super(Derived, self).save(commit=commit)
    

    However I have to admit this looks quite ugly and I would only use it if I feel I really need it.

提交回复
热议问题