Some syntactic sugar proposals
ladasky at my-deja.com
Tue Nov 16 23:32:19 CET 2010
On Nov 14, 11:30 pm, alex23 <wuwe... at gmail.com> wrote:
> On Nov 15, 4:39 pm, Dmitry Groshev <lambdadmi... at gmail.com> wrote:
> > if x in range(a, b): #wrong!
> Only in Python 3.x, it's perfectly valid in Python 2.x. To achieve the
> same in Python 3.x, try:
> if x in list(range(a, b,)): # BUT SEE MY COMMENT BELOW
> > it feels so natural to check it that way, but we have to write
> > if a <= x <= b
> > I understand that it's not a big deal, but it would be awesome to have
> > some optimisations - it's clearly possible to detect things like that
> > "wrong" one and fix it in a bytecode.
> This seems more like a pessimisation to me: your range version
> constructs a list just to do a single container check. That's a _lot_
> more cumbersome than two simple comparisons chained together.
Also: testing for the membership of x in a set is NOT the same thing
as testing using inequality operators. The inequality operators will
return True for any FLOATING-POINT value within the range (a...b), but
the set test will only return True for set members.
More information about the Python-list