3 ms·
Quick note, the more pythonic way of checking par in this case would be just to: if par: do_stuff() Python does some really cool, if slightly unintuitiv
by LostJourneyman 6y ago
Quick note, the more pythonic way of checking par in this case would be just to:
if par:
do_stuff()
Python does some really cool, if slightly unintuitive, checks for "truthiness" where 0, [], {}, false, '' and None (could be other cases too, brain's a little foggy today) all evaluate as false. This controls for:
if par = None:
do_stuff()
not catching empty lists, empty dicts, etc.
- andi999 6y agoHow do you define the function in that case?
- LostJourneyman 6y agoSo, a more complete example might help: def myFunc(par=None): if par: # do whatever is needed when the parameter exists pass #finish processing This allows you to pass in [], {}, None, '', or omit par all together, which will all behave as if par=None, or pass in data and use it appropriately. You can also use if not par: pass if you want to do the inverse.