Python - 最简单的方式来覆盖__init__,在super()调用之后必须使用可选的kwarg? [英] Python - Cleanest way to override __init__ where an optional kwarg must be used after the super() call?

查看:842
本文介绍了Python - 最简单的方式来覆盖__init__,在super()调用之后必须使用可选的kwarg?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我喜欢python看起来很漂亮,感觉很好,我希望这样可以更干净(可读性非常棒)。在覆盖 init 的子类 c c>超级链接时,必须使用 ()电话?



我有一个django表单,我想接受一个可选的用户参数,但如果我将它定义为一个参数 user =无,则通常的表单调用 form({})假定位置参数是指关键字参数 user



代码比(我的)话更好:

  def __init __(self,* args,** kwargs):
user = None
if'user'in kwargs:
user = kwargs.pop('user')
super(BaseCheckoutForm,self).__ init __(* args,** kwargs)
如果用户:
self.prefill_from_user(user)

我可以通过查看实际的表单类来查看最新的参数,但是关于在python中对任何东西进行子类化的最大的事情之一是收集所有的 args kwargs 并将其传递给任何你子类的东西。此外,这并不适用于源的任何更改。

  def __init __(self,querydict = None,user = None )
super(BaseCheckoutForm,self).__ init __(querydict)
如果用户:
self.prefill_from_user(用户)

但不幸的是我有:

  def __init __(self,* args, ** kwargs):
#无法定义user = None作为参数,因为正常使用
#类期望某个位置参数的顺序
user = None
如果'user'在kwargs:
#必须弹出,因为super()__ init__不期待用户kwarg
user = kwargs.pop('user')
super(BaseCheckoutForm,self).__ init __ * args,** kwargs)
如果用户:
self.prefill_from_user(用户)

感谢任何输入!

解决方案

我通常只是做基本上你在做这些事情。但是,您可以通过向 dict.pop 提供默认参数来缩短/清理代码:

 user = kwargs.pop('user',None)
super(BaseCheckoutForm,self) args,** kwargs)
如果用户不是无:
self.prefill_from_user(用户)


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 overriding a subclassed init where the optional kwarg has to be used after the super() call?

I have a django form where I'd like to accept an optional user argument, but if I define it as one of the arguments user=None, then the usual form call form({}) assumes the positional argument refers to the keyword argument user.

Code speaks better than (my) words:

def __init__(self, *args, **kwargs):
    user = None
    if 'user' in kwargs:
        user = kwargs.pop('user')
    super(BaseCheckoutForm, self).__init__(*args, **kwargs)
    if user:
        self.prefill_from_user(user)

I can make this the cleanest by looking into the actual Form class to see what arguments it's looking for, but one of the greatest things about subclassing anything in python is collecting all args and kwargs and passing it into whatever you subclassed. Also this doesn't hold up to any changes in the source.

def __init__(self, querydict=None, user=None):
    super(BaseCheckoutForm, self).__init__(querydict)
    if user:
        self.prefill_from_user(user)

But unfortunately I have:

 def __init__(self, *args, **kwargs):
    # cannot define user=None as an argument because normal usage
    # of class expects a certain order of positional args
    user = None
    if 'user' in kwargs:
        # must pop now since super()__init__ isn't expecting a user kwarg
        user = kwargs.pop('user') 
    super(BaseCheckoutForm, self).__init__(*args, **kwargs)
    if user:
        self.prefill_from_user(user)

Thanks for any input!

解决方案

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)

这篇关于Python - 最简单的方式来覆盖__init__,在super()调用之后必须使用可选的kwarg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆