I have a definition like this
def bar(self, foo=None, bar=None, baz=None):
pass
I want to make sure a maximum of one of foo, bar, baz is passed. I can do
if foo and bar:
raise Ex()
if foo and baz:
raise Ex()
....
But there got be something simpler.
How about:
initialisers = [foo, bar, baz]
if initialisers.count(None) < len(initialisers) - 1:
raise Ex()
It simply counts how many None
are present. If they're all None
or only one isn't then fine, otherwise it raises the exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With