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

60πŸ‘

βœ…

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)
πŸ‘€Joe Kington

Leave a comment