python - Order of syntax for using 'not' and 'in' keywords -


when testing membership, can use:

x not in y 

or alternatively:

not y in x 

there can many possible contexts expression depending on x , y. substring check, list membership, dict key existence, example.

  • are 2 forms equivalent?
  • is there preferred syntax?

they give same result.

in fact, not 'ham' in 'spam , eggs' appears special cased perform single "not in" operation, rather "in" operation , negating result:

>>> import dis  >>> def notin():     'ham' not in 'spam , eggs' >>> dis.dis(notin)   2           0 load_const               1 ('ham')               3 load_const               2 ('spam , eggs')               6 compare_op               7 (not in)               9 pop_top                           10 load_const               0 (none)              13 return_value      >>> def not_in():     not 'ham' in 'spam , eggs' >>> dis.dis(not_in)   2           0 load_const               1 ('ham')               3 load_const               2 ('spam , eggs')               6 compare_op               7 (not in)               9 pop_top                           10 load_const               0 (none)              13 return_value      >>> def not__in():     not ('ham' in 'spam , eggs') >>> dis.dis(not__in)   2           0 load_const               1 ('ham')               3 load_const               2 ('spam , eggs')               6 compare_op               7 (not in)               9 pop_top                           10 load_const               0 (none)              13 return_value          >>> def noteq():     not 'ham' == 'spam , eggs' >>> dis.dis(noteq)   2           0 load_const               1 ('ham')               3 load_const               2 ('spam , eggs')               6 compare_op               2 (==)               9 unary_not                         10 pop_top                           11 load_const               0 (none)              14 return_value       

i had thought @ first gave same result, not on own low precedence logical negation operator, applied a in b other boolean expression, whereas not in separate operator convenience , clarity.

the disassembly above revealing! seems while not logical negation operator, form not in b special cased it's not using general operator. makes not in b literally same expression a not in b, rather merely expression results in same value.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -