【发布时间】:2017-02-14 03:02:00
【问题描述】:
我有以下方法:
def a(b='default_b', c='default_c', d='default_d'):
# …
pass
我有一些命令行界面,允许提供变量 user_b、user_c、user_d 或将它们设置为 None - 即简单的 Python argparse 模块。
我想拨打这样的电话:
a(b=user_b if user_b is not None,
c=user_c if user_c is not None,
d=user_d if user_d is not None)
如果变量是None,我想使用方法参数中的默认值。
我发现的唯一方法是检查用户变量的所有组合:
if not user_b and not user_c and not user_d:
a()
elif not user_b and not user_c and user_d:
a(d)
…
elif user_b and user_c and user_d:
a(b, c, d)
有什么更高效、更花哨和 Pythonic 的方式来解决我的问题?
【问题讨论】: