Some syntactic sugar proposals

Andreas Löscher andreas.loescher at s2005.tu-chemnitz.de
Mon Nov 22 12:21:42 CET 2010


>     if x in range(a, b): #wrong!
> 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.


You can implement it yourself:

class between(object):
	def __init__(self, a,b):
		super(crang, self).__init__()
		self.a=a
		self.b=b
	def __contains__(self, value):
		return self.a <= value <= self.b

>>> 12.45 in between(-100,100)
true


But do you need

a <  x <  b
a <= x <  b
a <= x <= b or
a <  x <= b ?

Sure, you could set a new parameter for this, but the normal way is not
broken at all.

Best





More information about the Python-list mailing list