not in

Michael Robin me at mikerobin.com
Mon Jan 21 16:39:00 EST 2002


Neal Norwitz <neal at metaslash.com> wrote in message 
> rihad wrote:
> > 
> > Why does python introduce new syntax with `x not in s'? Isn't it the
> > same as `not (x in s)'? Same for `is not'.
> 
> Not exactly.  'not in' and 'not is' are operators, just like 'in' and 'is'.
> The byte codes that are generated are slightly different.  not (...)
> generates an extra byte code.
> 
> However, 'not x in s' should equal 'x not in s'.
> 
> Use whichever is clearer to you.  I prefer x not in s, because that's
> how I use it in English and how I think.

They should evaluate equal, but you'd think you could choose
one over the other based on wether you expected the item
to normally be in the sequence (early) or not, as "not x in s"
should drop out faster than "x not in x" if you expect
one or more x's to be present, and conversely. (I just verified
this with list of len==1000000 in a loop.)
Probably not an issue for any but the most heinous instantiated
sequences, but for getitem()s/iter()s it could be a realizable savings.

in not control,
mike

(reminds me of: 
in Just- spring when the world is mud-luscious)



More information about the Python-list mailing list