gpt4 book ai didi

带有太多默认值参数的 Python 函数,如何使它更干净?

转载 作者:行者123 更新时间:2023-11-28 20:53:33 24 4
gpt4 key购买 nike

我有以下函数签名,它看起来真的很丑,我该怎么做才能让它看起来更干净?

def contact(
request, sender=settings.DEFAULT_FROM_EMAIL,
subj_tmpl='contato/subject.txt',msg_tmpl='contato/msg.html',
template='contato/contato.html', success_template='contato/success.html',
success_redir='/',append_message=None,):

最佳答案

如果我是你,我想我会这样做:

def contact(request, sender=None, append_message=None, context=None):

if not sender:
sender = settings.DEFAULT_FROM_EMAIL # i hope that you can access settings here

# The context arg is a dictionary where you can put all the others argument and
# you can use it like so :

subj_tmpl = context.get('subj_tmpl', 'contato/subject.txt')
# ....

希望对您有所帮助。

关于带有太多默认值参数的 Python 函数,如何使它更干净?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4067520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com