Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if with range
#6
I would be rather restrained when using range(a, b) to check if some number is between a and b. For python 2 it could be really atracious - what will happen if OP would want to check not the range 1 to 200, but the range 1 to 2000000000? And while for python 3 it works well, I still see it working well due to a "internal trick" and not working as intended ("testing for membership").

When python3 interprets x in range(a, b) and x is integer, then  range's __contains__() method does not even try to use the range as an iterable/sequence/whatever, instead of it just checks a <= x < b (for step != 1 constraints would be different). So why use x in range(a, b) instead of checking these conditions directly? And if you need to check if x is an integral, then you should check it anyway (except "tiny" ranges) - try 7.5 in range(0, 1000000000).

Disclaimer: as quite often, it is possible that I dont know what I am talking about
Reply


Messages In This Thread
if with range - by Federer - Mar-08-2017, 08:36 PM
RE: if with range - by nilamo - Mar-08-2017, 08:41 PM
RE: if with range - by zivoni - Mar-08-2017, 08:43 PM
RE: if with range - by merlem - Mar-09-2017, 04:49 PM
RE: if with range - by buran - Mar-09-2017, 05:54 PM
RE: if with range - by zivoni - Mar-09-2017, 08:36 PM
RE: if with range - by buran - Mar-09-2017, 09:44 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020