考虑:
def foobar(*, foo, bar):
if foo:
print('foo', end="")
if bar:
print('bar', end="")
if foo and bar:
print('No bueno', end='') # I want this to be impossible
if not foo and not bar:
print('No bueno', end='') # I want this to be impossible
print('')
foobar(foo='bar') # I want to pass inspection
foobar(bar='foo') # I want to pass inspection
foobar(foo='bar', bar='foo') # I want to fail inspection
foobar() # I want to fail inspection
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以设置一个函数,使调用它的方法仅在仅传递foo或bar时才通过检查,而无需手动检查函数内部?
python optional-parameters optional-arguments keyword-argument python-3.x