python - if a == b or a == c: vs if a in {b, c}: -
in code used have comparisons if == b or == c or == d:
frequently. @ point discovered these shortened if in {b, c, d}:
or if in (b, c, d):
if values aren't hashable. however, have never seen such construction in else's code. because either:
- the
==
way slower. - the
==
way more pythonic. - they subtly different things.
- i have, chance, not looked @ code required either.
- i have seen , ignored or forgotten it.
- one shouldn't need have comparisons because one's code sould better elsewhere.
nobody has thought ofin
way except me.
which reason, if any, it?
for simple values (i.e. not expressions or nan
s), if == b or == c
, if in <iterable of b , c>
equivalent.
if values hashable, it's better use in
set literal instead of tuple or list literals:
if in {b, c}: ...
cpython's peephole optimiser able replace cached frozenset()
object, , membership tests against sets o(1) operations.
wiki
Comments
Post a Comment