[Python-ideas] Re: Circular Indexing

2020-11-26 Thread Steven D'Aprano
On Thu, Nov 26, 2020 at 01:27:46PM +1300, Greg Ewing wrote: > On 26/11/20 12:41 pm, Steven D'Aprano wrote: > > a = "abcdef" > > a[-2] # returns a result > > Yes, but did you *intend* that result, or did the -2 > result from a calculation that should have returned a > positive index but we

[Python-ideas] Re: Circular Indexing

2020-11-25 Thread Paul Sokolovsky
Hello, On Thu, 26 Nov 2020 13:27:46 +1300 Greg Ewing wrote: > On 26/11/20 12:41 pm, Steven D'Aprano wrote: > > a = "abcdef" > > a[-2] # returns a result > > Yes, but did you *intend* that result, or did the -2 > result from a calculation that should have returned a > positive index

[Python-ideas] Re: Circular Indexing

2020-11-25 Thread Greg Ewing
On 26/11/20 12:41 pm, Steven D'Aprano wrote: a = "abcdef" a[-2] # returns a result Yes, but did you *intend* that result, or did the -2 result from a calculation that should have returned a positive index but went wrong? Python has no way to tell. -- Greg ___

[Python-ideas] Re: Circular Indexing

2020-11-25 Thread Eric V. Smith
On 11/25/2020 6:41 PM, Steven D'Aprano wrote: On Thu, Nov 26, 2020 at 12:07:56PM +1300, Greg Ewing wrote: On 26/11/20 2:30 am, nathan.w.edwa...@outlook.com wrote: At times I heavily rely on index out of bound exceptions to reduce the number of lines necessary for error checking. This is a do

[Python-ideas] Re: Circular Indexing

2020-11-25 Thread Eric Fahlgren
On Wed, Nov 25, 2020 at 3:44 PM Steven D'Aprano wrote: > Obviously you can tell the two apart, so I'm confused by your comment. > What I imagined while reading Greg's comment was trying to explain to a student why this didn't work the way they expected. "Ok, so in the first case I'm *not* start

[Python-ideas] Re: Circular Indexing

2020-11-25 Thread Steven D'Aprano
On Thu, Nov 26, 2020 at 12:07:56PM +1300, Greg Ewing wrote: > On 26/11/20 2:30 am, nathan.w.edwa...@outlook.com wrote: > >At times I heavily rely on index out of bound exceptions to reduce the > >number of lines necessary for error checking. > > This is a downside to the negative indexing scheme

[Python-ideas] Re: Circular Indexing

2020-11-25 Thread Greg Ewing
On 26/11/20 2:30 am, nathan.w.edwa...@outlook.com wrote: At times I heavily rely on index out of bound exceptions to reduce the number of lines necessary for error checking. This is a downside to the negative indexing scheme -- you can't tell the difference between a backwards index and an erro

[Python-ideas] Re: Circular Indexing

2020-11-25 Thread nathan . w . edwards
The power in programming is the simplicity in keyword and instruction function, I do believe. As much as I love the concept, I feel the use of a data structure to handle such cases circular indexing is needed is more appropriate than changing loop statement behavior.At times I heavily rely on index

[Python-ideas] Re: Circular Indexing and FOR loop minimization

2020-11-25 Thread Serhiy Storchaka
25.11.20 06:29, Mathew M. Noel via Python-ideas пише: > 2.) If circular indexing is used then instead of using a double FOR loop > to go through a loop twice we can iterate from 0 to 2n ! If you just need to iterate list indices twice, iterate range(-n, n) instead of range(n).

[Python-ideas] Re: Circular Indexing and FOR loop minimization

2020-11-25 Thread Steven D'Aprano
Hi Mathew, welcome! My responses interleaved with your comments. On Wed, Nov 25, 2020 at 04:29:17AM +, Mathew M. Noel via Python-ideas wrote: > As discussed earlier on the post on 'Circular Indexing', considering > interpreting indices that lie outside the range 0 to n-1 modulo n for > li

[Python-ideas] Re: Circular Indexing

2020-11-25 Thread Paul Sokolovsky
Hello, On Tue, 24 Nov 2020 19:36:03 + "Mathew M. Noel via Python-ideas" wrote: > Python uses an index of -1 to index the last element in a list. Since > -1 occurs before 0 we might think of the elements of the linear list > are being bent into a circle making the last element occur before th

[Python-ideas] Re: Circular Indexing

2020-11-24 Thread Richard Damon
On 11/24/20 2:36 PM, Mathew M. Noel via Python-ideas wrote: > > Python uses an index of -1 to index the last element in a list. Since > -1 occurs before 0 we might think of the elements of the linear list > are being bent into a circle making the last element occur before the > 0th element. Conside

[Python-ideas] Re: Circular Indexing

2020-11-24 Thread Steele Farnsworth
I believe this would be simple to implement when it's needed by subclassing `collections.UserList` and wrapping this functionality around `__getitem__`, `__setitem__`, and `__delitem__`. On Tue, Nov 24, 2020, 2:38 PM Mathew M. Noel via Python-ideas < python-ideas@python.org> wrote: > Python uses

[Python-ideas] Re: Circular Indexing

2020-11-24 Thread edwin
November 24, 2020 2:36 PM, "Mathew M. Noel via Python-ideas" mailto:python-ideas@python.org?to=%22Mathew%20M.%20Noel%20via%20Python-ideas%22%20)> wrote: [snip] This feature can prove to be extremely useful. It can also prove to be extremely dangerous. "list index out of range" errors can