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:

  1. the == way slower.
  2. the == way more pythonic.
  3. they subtly different things.
  4. i have, chance, not looked @ code required either.
  5. i have seen , ignored or forgotten it.
  6. one shouldn't need have comparisons because one's code sould better elsewhere.
  7. nobody has thought of in way except me.

which reason, if any, it?

for simple values (i.e. not expressions or nans), 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

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -