a refactoring like this:
https://www.toptal.com/developers/hastebin/igutaripac.py
however, it got me thinking: is there a way to make the variable = variant1 or variant2 or variant3 or ... expression more concise?
i suppose i can write my own function like this:
def multiple_or(*args):
if len(args) == 1:
return args[0]
return multiple_or(*(args[:-1])) or args[-1]
but, is there a built-in function that does that?
any(...)
any returns a bool though, i need the item
you don't need it to be recursive then, though
You can also use itertools: from itertools import dropwhile from operator import not_ def multiple_or(seq, default=None): return next(dropwhile(not_, seq), default) You can also check out the "first_true" definition here: https://docs.python.org/3/library/itertools.html?highlight=iter#itertools-recipes
first_true is what i was looking for!! thanks :D
why import operator.not from operator import not and import operator operator.not is throwing syntax error
Sorry, i made a typo, it's "not_" and not "not"
ah ok its operator.not_(obj)
same with or_
yeah got it :D
Обсуждают сегодня