检查是否使用特定关键字调用函数
Check if function called with particular keyword
我想知道什么是最好的(最 pythonic and/or 优雅的)方式来仅在关键字参数被调用时对它进行操作。类似于:
import keyword
def (*arg, **kwargs):
if keyword.iskeyword(parameter):
# Do stuff involving parameter
# Do other stuff
return 0
检查预期的键是否在 kwargs
参数中:
def foo(*arg, **kwargs):
if 'bar' in kwargs:
print('You called foo with bar={}'.format(kwargs['bar']))
else:
print('You didn't use the "bar" keyword argument')
我想知道什么是最好的(最 pythonic and/or 优雅的)方式来仅在关键字参数被调用时对它进行操作。类似于:
import keyword
def (*arg, **kwargs):
if keyword.iskeyword(parameter):
# Do stuff involving parameter
# Do other stuff
return 0
检查预期的键是否在 kwargs
参数中:
def foo(*arg, **kwargs):
if 'bar' in kwargs:
print('You called foo with bar={}'.format(kwargs['bar']))
else:
print('You didn't use the "bar" keyword argument')