Python - Cleanest way to override __init__ where an optional kwarg must be used after the super() call?

后端 未结 1 1262
抹茶落季
抹茶落季 2020-12-13 13:11

I love how beautiful python looks/feels and I\'m hoping this can be cleaner (readability is awesome).

What\'s a clean way to accept an optional keyword argument when

相关标签:
1条回答
  • 2020-12-13 13:40

    I usually just do essentially what you're doing here. However, you can shorten/clean up your code by supplying a default argument to dict.pop:

     def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(BaseCheckoutForm, self).__init__(*args, **kwargs)
        if user is not None:
            self.prefill_from_user(user)
    
    0 讨论(0)
提交回复
热议问题