写类似下面的代码真的是 pain in ass:
def test(a, b, c=None, d=None, e=None):
ret = {
'a': a,
'b': b,
}
if c is not None:
ret['c'] = c
if d is not None:
ret['d'] = d
if e is not None:
ret['e'] = e
return ret
使用 optionaldict 就清爽多啦:
from optionaldict import optionaldict
def test(a, b, c=None, d=None, e=None):
ret = optionaldict(
a=a,
b=b,
c=c,
d=d,
e=e
)
return ret
# or if you prefer to return a built-in dict object:
# return dict(ret)