Re: range() is not the best way to check range?

2006-07-25 Thread Antoon Pardon
On 2006-07-23, Paul Boddie [EMAIL PROTECTED] wrote: Antoon Pardon wrote: Except that if you write your own class from scratch, you can't use it as a slice. Correct, but we were actually discussing subclassing built-in classes for use as a replacement for range/xrange. :-) I think a slice

Re: range() is not the best way to check range?

2006-07-23 Thread Antoon Pardon
On 2006-07-21, Paul Boddie [EMAIL PROTECTED] wrote: Regardless of whether myslice inherits from object or not, there's no persuading the interpreter that it is a genuine slice, and remember that we can't subclass slice (for some reason unknown). So, it would appear that the interpreter really

Re: range() is not the best way to check range?

2006-07-23 Thread Paul Boddie
Antoon Pardon wrote: Except that if you write your own class from scratch, you can't use it as a slice. Correct, but we were actually discussing subclassing built-in classes for use as a replacement for range/xrange. :-) It may be hard work writing all those methods in a totally new

Re: range() is not the best way to check range?

2006-07-21 Thread Antoon Pardon
On 2006-07-20, Paul Boddie [EMAIL PROTECTED] wrote: Alex Martelli wrote: Paul Boddie [EMAIL PROTECTED] wrote: Well, range is a function in the current implementation, although its usage is similar to that one would get if it were a class, particularly a subclass of list or one providing

Re: range() is not the best way to check range?

2006-07-21 Thread Paul Boddie
Antoon Pardon wrote: [Subclasses of list or slice for ranges] Except that if you write your own class from scratch, you can't use it as a slice. For a language that is supposed to be about duck typing I find it strange that if I make my own class with a start, stop and step attribute, that

Re: range() is not the best way to check range?

2006-07-21 Thread Steve Holden
Paul Boddie wrote: [...] Regardless of whether myslice inherits from object or not, there's no persuading the interpreter that it is a genuine slice, and remember that we can't subclass slice (for some reason unknown). So, it would appear that the interpreter really wants instances from some

Re: range() is not the best way to check range?

2006-07-20 Thread Alex Martelli
Paul Boddie [EMAIL PROTECTED] wrote: John Machin wrote: range() and xrange() are functions. You are suggesting that 2 *functions* should acquire a __contains__ method each? I trust not. Well, range is a function in the current implementation, although its usage is similar to that one

Re: range() is not the best way to check range?

2006-07-20 Thread Paul Boddie
Alex Martelli wrote: Paul Boddie [EMAIL PROTECTED] wrote: Well, range is a function in the current implementation, although its usage is similar to that one would get if it were a class, particularly a subclass of list or one providing a list-style interface. With such a class, you

Re: range() is not the best way to check range?

2006-07-20 Thread Alex Martelli
Paul Boddie [EMAIL PROTECTED] wrote: Alex Martelli wrote: Paul Boddie [EMAIL PROTECTED] wrote: Well, range is a function in the current implementation, although its usage is similar to that one would get if it were a class, particularly a subclass of list or one providing a

Re: range() is not the best way to check range?

2006-07-19 Thread Dan Bishop
Paul Boddie wrote: John Machin wrote: On 19/07/2006 1:05 AM, Dan Bishop wrote: xrange already has __contains__. As pointed out previously, xrange is a function and one would not expect it to have a __contains__ method. Well, you pointed out that range is a function, but xrange

Re: range() is not the best way to check range?

2006-07-18 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], tac-tics wrote: Grant Edwards wrote: for pete's sake use the comparison operator like god intended. if 0 = i = 1: I'm assuming you used Python's compound comparison as opposed to the C-style of and'ing two comparisons together to emphasize the fact it is

Re: range() is not the best way to check range?

2006-07-18 Thread Nick Craig-Wood
Grant Edwards [EMAIL PROTECTED] wrote: Creating and then searching a 10,000 element list to see if a number is between two other numbers is insane. Using xrange as somebody else suggested is also insane. Aye to both If you want to know if a number is between two other numders, for

Re: range() is not the best way to check range?

2006-07-18 Thread Paul Boddie
John Machin wrote: On 18/07/2006 12:41 PM, [EMAIL PROTECTED] wrote: it seems that range() can be really slow: [...] Some things to try: 1a. Read what the manual has to say about the range() function ... what does it produce? Indeed. Still, the addition of a __contains__ method to range

Re: range() is not the best way to check range?

2006-07-18 Thread Andy Dingley
[EMAIL PROTECTED] wrote: it seems that range() can be really slow: if i in range (0, 1): RTFM on range() You're completely mis-using it here, using it with an if ... in ... test. The purpose of range() in Python is as loop control, not comparisons! It's not a SQL BETWEEN statement.

Re: range() is not the best way to check range?

2006-07-18 Thread Leif K-Brooks
Grant Edwards wrote: Using xrange as somebody else suggested is also insane. Sorry about that, I somehow got the misguided notion that xrange defines its own __contains__, so that it would be about the same speed as using comparison operators directly. I figured the OP might have a better

Re: range() is not the best way to check range?

2006-07-18 Thread John Machin
On 18/07/2006 7:22 PM, Paul Boddie wrote: John Machin wrote: On 18/07/2006 12:41 PM, [EMAIL PROTECTED] wrote: it seems that range() can be really slow: [...] Some things to try: 1a. Read what the manual has to say about the range() function ... what does it produce? Indeed. Still,

Re: range() is not the best way to check range?

2006-07-18 Thread Paul Boddie
John Machin wrote: range() and xrange() are functions. You are suggesting that 2 *functions* should acquire a __contains__ method each? I trust not. Well, range is a function in the current implementation, although its usage is similar to that one would get if it were a class, particularly a

Re: range() is not the best way to check range?

2006-07-18 Thread Stefan Behnel
Andy Dingley wrote: The purpose of range() in Python is as loop control, No, the purpose of range() is to create a list, as the docs state. http://docs.python.org/lib/built-in-funcs.html range(...) - This is a versatile function to create lists containing arithmetic progressions. Stefan --

Re: range() is not the best way to check range?

2006-07-18 Thread Dan Bishop
Paul Boddie wrote: Yes, he wants range to return an iterator, just like xrange more or less does now. Given that xrange objects support __getitem__, unlike a lot of other iterators (and, of course, generators), adding __contains__ wouldn't be much of a hardship. Certainly, compared to other

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, tac-tics [EMAIL PROTECTED] wrote: Grant Edwards wrote: for pete's sake use the comparison operator like god intended. if 0 = i = 1: I'm assuming you used Python's compound comparison as opposed to the C-style of and'ing two comparisons together to emphasize the fact

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: In [EMAIL PROTECTED], tac-tics wrote: Grant Edwards wrote: for pete's sake use the comparison operator like god intended. if 0 = i = 1: I'm assuming you used Python's compound comparison as opposed to the C-style

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, Paul Boddie [EMAIL PROTECTED] wrote: John Machin wrote: range() and xrange() are functions. You are suggesting that 2 *functions* should acquire a __contains__ method each? I trust not. Well, range is a function in the current implementation, although its usage is similar to

Re: range() is not the best way to check range?

2006-07-18 Thread Steve Holden
Grant Edwards wrote: On 2006-07-18, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: In [EMAIL PROTECTED], tac-tics wrote: Grant Edwards wrote: for pete's sake use the comparison operator like god intended. if 0 = i = 1: I'm assuming you used Python's compound comparison as

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
Dan Bishop wrote: [EMAIL PROTECTED] wrote: it seems that range() can be really slow: ... if i in range (0, 1): This creates a 10,000-element list and sequentially searches it. Of course that's gonna be slow. And you're doing it 3 times. --

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
Nick Craig-Wood wrote: Sets are pretty fast too, and have the advantage of flexibility in that you can put any numbers in you like I know this is self-evident to most of the people reading this, but I thought it worth pointing out that this is a great way to test membership in range(lo, hi,

Re: range() is not the best way to check range?

2006-07-18 Thread Diez B. Roggisch
Simon Forman wrote: Nick Craig-Wood wrote: Sets are pretty fast too, and have the advantage of flexibility in that you can put any numbers in you like I know this is self-evident to most of the people reading this, but I thought it worth pointing out that this is a great way to test

Re: range() is not the best way to check range?

2006-07-18 Thread K.S.Sreeram
Simon Forman wrote: Nick Craig-Wood wrote: Sets are pretty fast too, and have the advantage of flexibility in that you can put any numbers in you like I know this is self-evident to most of the people reading this, but I thought it worth pointing out that this is a great way to test

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
Diez B. Roggisch wrote: Simon Forman wrote: Nick Craig-Wood wrote: Sets are pretty fast too, and have the advantage of flexibility in that you can put any numbers in you like I know this is self-evident to most of the people reading this, but I thought it worth pointing out that

Re: range() is not the best way to check range?

2006-07-18 Thread Paul Boddie
Grant Edwards wrote: It's unclear what you're referring to as the range. The notion of something describing a range of values which can be expanded to a list or, of relevance here, whose boundaries can be tested efficiently. Perhaps you're thinking of a slice? Somethign like if

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, Steve Holden [EMAIL PROTECTED] wrote: That said, for pete's sake is probably a just an cleaned up version of for god's sake, so I probably did call pete god. No, actually you called god pete ;-) Well that's certainly wrong, because we all know god's name is Howard. Our

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, Paul Boddie [EMAIL PROTECTED] wrote: It's unclear what you're referring to as the range. The notion of something describing a range of values which can be expanded to a list or, of relevance here, whose boundaries can be tested efficiently. Perhaps you're thinking of a

Re: range() is not the best way to check range?

2006-07-18 Thread bearophileHUGS
Dan Bishop: xrange already has __contains__. The problem is, it's implemented by a highly-inefficient sequential search. Why not modify it to merely check the bounds and (value - start) % step == 0? I think this is a nice idea. Bye, bearophile --

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
K.S.Sreeram wrote: Simon Forman wrote: Nick Craig-Wood wrote: Sets are pretty fast too, and have the advantage of flexibility in that you can put any numbers in you like I know this is self-evident to most of the people reading this, but I thought it worth pointing out that this is

Re: range() is not the best way to check range?

2006-07-18 Thread Summercoolness
[EMAIL PROTECTED] wrote: it seems that range() can be really slow: if i in range (0, 1): My original use was like this: if i in range (iStart, iEnd): listData.append(a) in which iStart is 1000 and iEnd is 1008 so in that case, the program ran fine... but later on, i

Re: range() is not the best way to check range?

2006-07-18 Thread tac-tics
Simon Forman wrote: To me, and perhaps others, T = set(xrange(0, 1, 23)) and n in T are somewhat easier to read and write than not n % 23 and 0 = n 1, YMMV. Eh? How is the first easier to read than the second?? You have a nested function call in the first! Regardless, testing if a

Re: range() is not the best way to check range?

2006-07-18 Thread John Machin
On 19/07/2006 1:05 AM, Dan Bishop wrote: Paul Boddie wrote: Yes, he wants range to return an iterator, just like xrange more or less does now. Given that xrange objects support __getitem__, unlike a lot of other iterators (and, of course, generators), adding __contains__ wouldn't be much of

Re: range() is not the best way to check range?

2006-07-18 Thread Paul Boddie
John Machin wrote: On 19/07/2006 1:05 AM, Dan Bishop wrote: xrange already has __contains__. As pointed out previously, xrange is a function and one would not expect it to have a __contains__ method. Well, you pointed out that range is a function, but xrange seems to be a type... xrange

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
tac-tics wrote: Simon Forman wrote: To me, and perhaps others, T = set(xrange(0, 1, 23)) and n in T are somewhat easier to read and write than not n % 23 and 0 = n 1, YMMV. Eh? How is the first easier to read than the second?? You have a nested function call in the first! I

range() is not the best way to check range?

2006-07-17 Thread Summercoolness
it seems that range() can be really slow: the following program will run, and the last line shows how long it ran for: import time startTime = time.time() a = 1.0 for i in range(0, 3): if i in range (0, 1): a += 1 if not i % 1000: print i print a,,

Re: range() is not the best way to check range?

2006-07-17 Thread Dan Bishop
[EMAIL PROTECTED] wrote: it seems that range() can be really slow: ... if i in range (0, 1): This creates a 10,000-element list and sequentially searches it. Of course that's gonna be slow. -- http://mail.python.org/mailman/listinfo/python-list

Re: range() is not the best way to check range?

2006-07-17 Thread Leif K-Brooks
[EMAIL PROTECTED] wrote: or is there an alternative use of range() or something similar that can be as fast? You could use xrange: [EMAIL PROTECTED]:~$ python -m timeit -n1 1 in range(1) 1 loops, best of 3: 260 usec per loop [EMAIL PROTECTED]:~$ python -m timeit -n1 1 in

Re: range() is not the best way to check range?

2006-07-17 Thread Dan Bishop
Leif K-Brooks wrote: [EMAIL PROTECTED] wrote: or is there an alternative use of range() or something similar that can be as fast? You could use xrange: [EMAIL PROTECTED]:~$ python -m timeit -n1 1 in range(1) 1 loops, best of 3: 260 usec per loop [EMAIL PROTECTED]:~$ python

Re: range() is not the best way to check range?

2006-07-17 Thread John Machin
On 18/07/2006 12:41 PM, [EMAIL PROTECTED] wrote: it seems that range() can be really slow: the following program will run, and the last line shows how long it ran for: import time startTime = time.time() a = 1.0 for i in range(0, 3): if i in range (0, 1): a +=

Re: range() is not the best way to check range?

2006-07-17 Thread K.S.Sreeram
[EMAIL PROTECTED] wrote: so if i change the line if i in range (0, 1): to if i = 0 and i 1: [snip;] is there an alternative use of range() or something similar that can be as fast? you've found that alternative yourself! just use the comparison operators... in fact, you

Re: range() is not the best way to check range?

2006-07-17 Thread Grant Edwards
On 2006-07-18, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: it seems that range() can be really slow: the following program will run, and the last line shows how long it ran for: import time startTime = time.time() a = 1.0 for i in range(0, 3): if i in range (0, 1):

Re: range() is not the best way to check range?

2006-07-17 Thread tac-tics
Grant Edwards wrote: for pete's sake use the comparison operator like god intended. if 0 = i = 1: I'm assuming you used Python's compound comparison as opposed to the C-style of and'ing two comparisons together to emphasize the fact it is god's chosen way of doing this ;-) --