[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 but went wrong? Python has no way to
> tell.

Certainly it does:

idx = index_calc()
assert idx >= 0

Compare that with the case where index of 5 is invalid (just can't
happen per algorithm invariants). Python (or any other language) "has
no way to tell" that just from looks of your algorithm, and yet it's
easy to tell that:

idx = index_calc()
assert idx != 5



-- 
Best regards,
 Paul  mailto:pmis...@gmail.com
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OLRBQRDWFB3QRVCW2Z2HAG66UOPTI5WS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Getting rid of FOR loops and simplifying cicular conviolutions with Circular Indexing

2020-11-25 Thread Antoine Rozo
Because it's not backward-incompatible with the behaviour of positive
indices.

Le jeu. 26 nov. 2020 à 05:56, Mathew M. Noel via Python-ideas <
python-ideas@python.org> a écrit :

>
> Why not use list_name[-n%N] whenever you need to use negative indices and
> raise an index out of bounds exception with negative indices like other
> programming languages?
>
>
>
>
> --
> *From:* Calvin Spealman 
> *Sent:* Thursday, November 26, 2020 1:00 AM
> *To:* Mathew M. Noel
> *Cc:* python-ideas@python.org; m...@pradyunsg.me
> *Subject:* Re: [Python-ideas] Getting rid of FOR loops and simplifying
> cicular conviolutions with Circular Indexing
>
>
> days_of_the_week[14 % 7]
>
> There ya go!
>
>
> On Wed, Nov 25, 2020 at 12:51 PM Mathew M. Noel via Python-ideas <
> python-ideas@python.org> wrote:
>
>> If circular indexing is used then instead of using a double FOR loop to
>> go through a list M times we can iterate from 0 to M*N (where N is the
>> length of the list) !!!
>>
>>
>> Almost all Machine Learning (ML) algorithms iterate for some predefined
>> epochs over a large data-set. So a double FOR loop is extremely common in
>> ML. Using circular indexing gets rid of this extra FOR loop. If we have to
>> iterate 2 times you can iterate using range(-n,n) but in most cases you
>> need to iterate over 10 or more epochs in ML.
>>
>>
>> Most scientific applications of Python involve an outer FOR loop which
>> progressively refines an approximation with an inner FOR loop by going
>> through a list of items. So circular indexing is useful. In the following I
>> discuss increasingly compelling reasons for adopting a circular indexing
>> scheme in Python.
>>
>>
>> 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.
>> Consider a list with n elements: it would be perfectly reasonable to
>> address the element 0 of the list using an index of n since n occurs after
>> n-1 (if we assume that the list is bent into a circle). This feature can
>> prove to be extremely useful. Consider the following example:
>>
>>
>> days_of_the_week = 
>> ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
>>
>> It would be nice if
>>
>> days_of_the_week[0]
>>
>> is the same as
>>
>> days_of_the_week[7]
>>
>> is the same as
>>
>> days_of_the_week[14] etc
>>
>> In other words use modular indexing. In other words if the index is outside 
>> the range 0 to n-1, we simply take the remainder when the index is divided 
>> by n as the index.
>> Because of the close relationship between finite length sequences and 
>> periodic sequences this feature might simplify scientific computing(circular 
>> convolution etc).
>>
>> If circular indexing is used then we don't need the arbitrary rule that
>> -1 is the index of the last element. Since -1 is the same as n-1
>> automatically in modular arithmetic.
>>
>>
>> A trivial objection:  "why not use list_name[i%n] whenever we need this
>> feature?" By the same token we could do away with negative indices and use
>> -1%n for example when we need to index with -1!
>>
>> Its unclear why that people have an irrational preference for indices
>> that lie to the left of 0 while strongly rejecting the idea of indices that
>> lie to the right of n-1!
>>
>> Python does not raise a "index out of bound" exception for negative
>> indices like other programming languages. If this negative indexing is a
>> "feature" (although it allows some fatal errors to slip) then indices above
>> n-1 can also be considered a feature!
>>
>> Are there any deep mathematical reasons for adopting  circular
>> convention?
>> Circular convolution is a most important operation in a wide variety of
>> scientific disciplines since the Discrete Fourier Transform (DFT) of the
>> circular convolution of two signals is the product of the transforms.
>> Because of the universal applicability of Fourier ideas in science and the
>> close mathematical relationship between finite length and periodic
>> sequences circular indexing is extensively used in signal processing and
>> mathematics.
>>
>> We can extend the idea of circular indexing to multidimensional arrays. A
>> 2D array can be folded into a cylinder for indexing. Further this cylinder
>> can be folded into a toroid to reduce a triple FOR loop to a single FOR
>> loop. A deep mathematical justification for cylindrical indexing of 2D and
>> in general nD arrays is offered by the fact that n-dimensional DFT reduces
>> n-dimensional circular convolution to element-wise multiplication.
>>
>> ___
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> 

[Python-ideas] Re: Making the for statement work better with nested functions

2020-11-25 Thread Guido van Rossum
Hmm... In the end I think the language design issue is with functions (and
how they capture variable references rather than values), and fixing it by
changing the for-loop is still just a band-aid, with other problems and
inconsistencies. Agreed that the fix 'x=x' essentially always works, and
that having to write 'for new x in ...' is no better, because you'd still
forget it.

Maybe we should consider introducing a new kind of function that captures
values instead? That would be a much more principled fix.

--Guido

On Thu, Nov 19, 2020 at 4:20 PM Ben Rudiak-Gould 
wrote:

> On Wed, Nov 18, 2020 at 9:54 PM Greg Ewing 
> wrote:
>
>> Essentially the idea is this: If the loop body contains a def or lambda
>> that captures the loop variable, then on each iteration, a new binding
>> is created, instead of changing an existing binding.
>>
>> Note that this is *not* the same as introducing a new scope. All the
>> bindings share the same name, and from the perspective of code outside
>> the nested function, nothing changes -- the name is still bound to
>> the most recent value, and can still be accessed after the loop has
>> finished.
>>
>
> The problem I see is that nested functions referring to the same variable
> can be defined outside loops as well. If you define a helper before the
> loop that refers to the loop variable, it won't work inside the loop or
> ever again after the loop has terminated. Even if the helper has nothing to
> do with the loop and just uses the same generic variable name like "item",
> it'll break. It's fairly common to reuse the same identifier for different
> purposes inside functions.
>
> As much as I hate the current behavior, I feel like the proposed behavior
> is too confusing and weird.
>
> Another problem is that the capturing behavior would depend on whether the
> variable is in a function or module or class scope, since cells only exist
> in function scopes.
>
> Also, "while x := get_next_thing():" loops wouldn't behave consistently
> with for loops.
>
> for new x in some_iterator:
>>...
>>
>
> I don't think special syntax would help. I've been bitten by this problem
> fairly often, and 100% of the time I've been able to solve it by adding x=x
> to the argument list to capture by value. I can't imagine I'd ever want to
> capture a loop variable by reference, especially if the cell being
> referenced no longer has a name outside of the nested function when the
> nested function is called. I get in trouble because I forget to write x=x
> where it's necessary, but if I forget to write x=x then I'd forget to write
> "new x" too.
>
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/VKHB722PLUMZJPCMHEM2MB4HBEDI463Y/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/H7S3LEVRHGPLLGVPVSHD54THO2G2PDY3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Getting rid of FOR loops and simplifying cicular conviolutions with Circular Indexing

2020-11-25 Thread Mathew M. Noel via Python-ideas

Why not use list_name[-n%N] whenever you need to use negative indices and raise 
an index out of bounds exception with negative indices like other programming 
languages?




From: Calvin Spealman 
Sent: Thursday, November 26, 2020 1:00 AM
To: Mathew M. Noel
Cc: python-ideas@python.org; m...@pradyunsg.me
Subject: Re: [Python-ideas] Getting rid of FOR loops and simplifying cicular 
conviolutions with Circular Indexing


days_of_the_week[14 % 7]


There ya go!

On Wed, Nov 25, 2020 at 12:51 PM Mathew M. Noel via Python-ideas 
mailto:python-ideas@python.org>> wrote:

If circular indexing is used then instead of using a double FOR loop to go 
through a list M times we can iterate from 0 to M*N (where N is the length of 
the list) !!!


Almost all Machine Learning (ML) algorithms iterate for some predefined epochs 
over a large data-set. So a double FOR loop is extremely common in ML. Using 
circular indexing gets rid of this extra FOR loop. If we have to iterate 2 
times you can iterate using range(-n,n) but in most cases you need to iterate 
over 10 or more epochs in ML.


Most scientific applications of Python involve an outer FOR loop which 
progressively refines an approximation with an inner FOR loop by going through 
a list of items. So circular indexing is useful. In the following I discuss 
increasingly compelling reasons for adopting a circular indexing scheme in 
Python.


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. Consider a list 
with n elements: it would be perfectly reasonable to address the element 0 of 
the list using an index of n since n occurs after n-1 (if we assume that the 
list is bent into a circle). This feature can prove to be extremely useful. 
Consider the following example:


days_of_the_week = 
["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

It would be nice if

days_of_the_week[0]

is the same as

days_of_the_week[7]

is the same as

days_of_the_week[14] etc

In other words use modular indexing. In other words if the index is outside the 
range 0 to n-1, we simply take the remainder when the index is divided by n as 
the index.
Because of the close relationship between finite length sequences and periodic 
sequences this feature might simplify scientific computing(circular convolution 
etc).

If circular indexing is used then we don't need the arbitrary rule that -1 is 
the index of the last element. Since -1 is the same as n-1 automatically in 
modular arithmetic.


A trivial objection:  "why not use list_name[i%n] whenever we need this 
feature?" By the same token we could do away with negative indices and use -1%n 
for example when we need to index with -1!

Its unclear why that people have an irrational preference for indices that lie 
to the left of 0 while strongly rejecting the idea of indices that lie to the 
right of n-1!

Python does not raise a "index out of bound" exception for negative indices 
like other programming languages. If this negative indexing is a "feature" 
(although it allows some fatal errors to slip) then indices above n-1 can also 
be considered a feature!

Are there any deep mathematical reasons for adopting  circular convention?
Circular convolution is a most important operation in a wide variety of 
scientific disciplines since the Discrete Fourier Transform (DFT) of the 
circular convolution of two signals is the product of the transforms. Because 
of the universal applicability of Fourier ideas in science and the close 
mathematical relationship between finite length and periodic sequences circular 
indexing is extensively used in signal processing and mathematics.

We can extend the idea of circular indexing to multidimensional arrays. A 2D 
array can be folded into a cylinder for indexing. Further this cylinder can be 
folded into a toroid to reduce a triple FOR loop to a single FOR loop. A deep 
mathematical justification for cylindrical indexing of 2D and in general nD 
arrays is offered by the fact that n-dimensional DFT reduces n-dimensional 
circular convolution to element-wise multiplication.

___
Python-ideas mailing list -- 
python-ideas@python.org
To unsubscribe send an email to 
python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5TJYKFLBHB26WEFFQXMY6AGWS34XTIUR/
Code of Conduct: http://python.org/psf/codeofconduct/


--

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

cspea...@redhat.com  M: 
+1.336.210.5107

[https://red.ht/sig]
TRIED. TESTED. TRUSTED.

[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 mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/B4QZX4WJR3PSXW6CSPYRSBL6636RVKKX/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 downside to the negative indexing scheme -- you
can't tell the difference between a backwards index and an
error due to out-of-bounds indexing.

Sorry, perhaps I am being a bit dim-witted this morning, but I don't
understand this. Surely the difference is obvious?

 a = "abcdef"
 a[-2]  # returns a result
 a[999]  # raises an exception

Obviously you can tell the two apart, so I'm confused by your comment.


If they're literals, it's easy.

But a[an_index-an_offset] isn't so easy to spot, say if you're using the 
wrong offset for example. In other programming languages, it might be an 
error for the index to be negative. In Python it's not, if the result <= 
-length(a). Sure, you could use an assert, but many people won't, and 
the assert can be optimized away.


I've been burned by this a number of times.

Eric
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FUCXVRIZFL7ZGEXJYDVH3U7DWMLRFZCB/
Code of Conduct: http://python.org/psf/codeofconduct/


[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* starting at the beginning?  I'm starting in the
*middle???  *That
makes no sense."

forward = False
data = [1, 2, 3, 4]

if forward:
# Start at the beginning and count up.
idx = 0
step = 1
else:
# Start at the end and count down.
idx = len(data) - 1
step = -1

while True:
try:
print(data[idx])
except IndexError:
break
else:
idx += step
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/S7QJDCMSB7KWX5AYT5VJTUFVEHUMX5YF/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 -- you
> can't tell the difference between a backwards index and an
> error due to out-of-bounds indexing.

Sorry, perhaps I am being a bit dim-witted this morning, but I don't 
understand this. Surely the difference is obvious?

a = "abcdef"
a[-2]  # returns a result
a[999]  # raises an exception

Obviously you can tell the two apart, so I'm confused by your comment.

Valid indices are the half-open interval `-n <= index < n` where n is 
the length of the sequence. Outside of that interval, you get an 
exception.


-- 
Steve
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QQ5EFB6525XZRMVCNAEX3RDNM62MMY3U/
Code of Conduct: http://python.org/psf/codeofconduct/


[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
error due to out-of-bounds indexing. Making all indexing
circular would just make this problem heaps worse.

I would have preferred a different syntax for backwards
indexing instead of inferring it from the index value,
but we're stuck with it now.

--
Greg
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/H63U2OIFC3PIDL3SCXYO3C5BRVF24WTS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Getting rid of FOR loops and simplifying cicular conviolutions with Circular Indexing

2020-11-25 Thread David Mertz
You've started three separate threads to propose something that has exactly
zero chance of happening, and would be of limited use in uncommon cases.
And that would break literally billions of lines of working code.

If you want the modulo operator, you are more than welcome to use it. If
you want to subclass list, have at it.

On Wed, Nov 25, 2020, 12:48 PM Mathew M. Noel via Python-ideas <
python-ideas@python.org> wrote:

> If circular indexing is used then instead of using a double FOR loop to
> go through a list M times we can iterate from 0 to M*N (where N is the
> length of the list) !!!
>
>
> Almost all Machine Learning (ML) algorithms iterate for some predefined
> epochs over a large data-set. So a double FOR loop is extremely common in
> ML. Using circular indexing gets rid of this extra FOR loop. If we have to
> iterate 2 times you can iterate using range(-n,n) but in most cases you
> need to iterate over 10 or more epochs in ML.
>
>
> Most scientific applications of Python involve an outer FOR loop which
> progressively refines an approximation with an inner FOR loop by going
> through a list of items. So circular indexing is useful. In the following I
> discuss increasingly compelling reasons for adopting a circular indexing
> scheme in Python.
>
>
> 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.
> Consider a list with n elements: it would be perfectly reasonable to
> address the element 0 of the list using an index of n since n occurs after
> n-1 (if we assume that the list is bent into a circle). This feature can
> prove to be extremely useful. Consider the following example:
>
>
> days_of_the_week = 
> ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
>
> It would be nice if
>
> days_of_the_week[0]
>
> is the same as
>
> days_of_the_week[7]
>
> is the same as
>
> days_of_the_week[14] etc
>
> In other words use modular indexing. In other words if the index is outside 
> the range 0 to n-1, we simply take the remainder when the index is divided by 
> n as the index.
> Because of the close relationship between finite length sequences and 
> periodic sequences this feature might simplify scientific computing(circular 
> convolution etc).
>
> If circular indexing is used then we don't need the arbitrary rule that -1
> is the index of the last element. Since -1 is the same as n-1 automatically
> in modular arithmetic.
>
>
> A trivial objection:  "why not use list_name[i%n] whenever we need this
> feature?" By the same token we could do away with negative indices and use
> -1%n for example when we need to index with -1!
>
> Its unclear why that people have an irrational preference for indices
> that lie to the left of 0 while strongly rejecting the idea of indices that
> lie to the right of n-1!
>
> Python does not raise a "index out of bound" exception for negative
> indices like other programming languages. If this negative indexing is a
> "feature" (although it allows some fatal errors to slip) then indices above
> n-1 can also be considered a feature!
>
> Are there any deep mathematical reasons for adopting  circular convention?
> Circular convolution is a most important operation in a wide variety of
> scientific disciplines since the Discrete Fourier Transform (DFT) of the
> circular convolution of two signals is the product of the transforms.
> Because of the universal applicability of Fourier ideas in science and the
> close mathematical relationship between finite length and periodic
> sequences circular indexing is extensively used in signal processing and
> mathematics.
>
> We can extend the idea of circular indexing to multidimensional arrays. A
> 2D array can be folded into a cylinder for indexing. Further this cylinder
> can be folded into a toroid to reduce a triple FOR loop to a single FOR
> loop. A deep mathematical justification for cylindrical indexing of 2D and
> in general nD arrays is offered by the fact that n-dimensional DFT reduces
> n-dimensional circular convolution to element-wise multiplication.
>
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/5TJYKFLBHB26WEFFQXMY6AGWS34XTIUR/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QPNCULY5HEU3EMMAYY5EYTZCYATBMGZP/
Code of Conduct: 

[Python-ideas] Re: Getting rid of FOR loops and simplifying cicular conviolutions with Circular Indexing

2020-11-25 Thread Calvin Spealman
days_of_the_week[14 % 7]

There ya go!


On Wed, Nov 25, 2020 at 12:51 PM Mathew M. Noel via Python-ideas <
python-ideas@python.org> wrote:

> If circular indexing is used then instead of using a double FOR loop to
> go through a list M times we can iterate from 0 to M*N (where N is the
> length of the list) !!!
>
>
> Almost all Machine Learning (ML) algorithms iterate for some predefined
> epochs over a large data-set. So a double FOR loop is extremely common in
> ML. Using circular indexing gets rid of this extra FOR loop. If we have to
> iterate 2 times you can iterate using range(-n,n) but in most cases you
> need to iterate over 10 or more epochs in ML.
>
>
> Most scientific applications of Python involve an outer FOR loop which
> progressively refines an approximation with an inner FOR loop by going
> through a list of items. So circular indexing is useful. In the following I
> discuss increasingly compelling reasons for adopting a circular indexing
> scheme in Python.
>
>
> 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.
> Consider a list with n elements: it would be perfectly reasonable to
> address the element 0 of the list using an index of n since n occurs after
> n-1 (if we assume that the list is bent into a circle). This feature can
> prove to be extremely useful. Consider the following example:
>
>
> days_of_the_week = 
> ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
>
> It would be nice if
>
> days_of_the_week[0]
>
> is the same as
>
> days_of_the_week[7]
>
> is the same as
>
> days_of_the_week[14] etc
>
> In other words use modular indexing. In other words if the index is outside 
> the range 0 to n-1, we simply take the remainder when the index is divided by 
> n as the index.
> Because of the close relationship between finite length sequences and 
> periodic sequences this feature might simplify scientific computing(circular 
> convolution etc).
>
> If circular indexing is used then we don't need the arbitrary rule that -1
> is the index of the last element. Since -1 is the same as n-1 automatically
> in modular arithmetic.
>
>
> A trivial objection:  "why not use list_name[i%n] whenever we need this
> feature?" By the same token we could do away with negative indices and use
> -1%n for example when we need to index with -1!
>
> Its unclear why that people have an irrational preference for indices
> that lie to the left of 0 while strongly rejecting the idea of indices that
> lie to the right of n-1!
>
> Python does not raise a "index out of bound" exception for negative
> indices like other programming languages. If this negative indexing is a
> "feature" (although it allows some fatal errors to slip) then indices above
> n-1 can also be considered a feature!
>
> Are there any deep mathematical reasons for adopting  circular convention?
> Circular convolution is a most important operation in a wide variety of
> scientific disciplines since the Discrete Fourier Transform (DFT) of the
> circular convolution of two signals is the product of the transforms.
> Because of the universal applicability of Fourier ideas in science and the
> close mathematical relationship between finite length and periodic
> sequences circular indexing is extensively used in signal processing and
> mathematics.
>
> We can extend the idea of circular indexing to multidimensional arrays. A
> 2D array can be folded into a cylinder for indexing. Further this cylinder
> can be folded into a toroid to reduce a triple FOR loop to a single FOR
> loop. A deep mathematical justification for cylindrical indexing of 2D and
> in general nD arrays is offered by the fact that n-dimensional DFT reduces
> n-dimensional circular convolution to element-wise multiplication.
>
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/5TJYKFLBHB26WEFFQXMY6AGWS34XTIUR/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

cspea...@redhat.com  M: +1.336.210.5107
[image: https://red.ht/sig] 
TRIED. TESTED. TRUSTED. 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JX2MJS5Y5GH4DMKGVE6N2GTFAUPCVTRJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Getting rid of FOR loops and simplifying cicular conviolutions with Circular Indexing

2020-11-25 Thread Antoine Rozo
So why don't you implement your own type/wrapper with the semantics you need?

Le mer. 25 nov. 2020 à 18:49, Mathew M. Noel via Python-ideas
 a écrit :
>
> If circular indexing is used then instead of using a double FOR loop to go 
> through a list M times we can iterate from 0 to M*N (where N is the length of 
> the list) !!!
>
>
> Almost all Machine Learning (ML) algorithms iterate for some predefined 
> epochs over a large data-set. So a double FOR loop is extremely common in ML. 
> Using circular indexing gets rid of this extra FOR loop. If we have to 
> iterate 2 times you can iterate using range(-n,n) but in most cases you need 
> to iterate over 10 or more epochs in ML.
>
>
> Most scientific applications of Python involve an outer FOR loop which 
> progressively refines an approximation with an inner FOR loop by going 
> through a list of items. So circular indexing is useful. In the following I 
> discuss increasingly compelling reasons for adopting a circular indexing 
> scheme in Python.
>
>
> 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. 
> Consider a list with n elements: it would be perfectly reasonable to address 
> the element 0 of the list using an index of n since n occurs after n-1 (if we 
> assume that the list is bent into a circle). This feature can prove to be 
> extremely useful. Consider the following example:
>
>
> days_of_the_week = 
> ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
>
> It would be nice if
>
> days_of_the_week[0]
>
>  is the same as
>
> days_of_the_week[7]
>
> is the same as
>
> days_of_the_week[14] etc
>
> In other words use modular indexing. In other words if the index is outside 
> the range 0 to n-1, we simply take the remainder when the index is divided by 
> n as the index.
> Because of the close relationship between finite length sequences and 
> periodic sequences this feature might simplify scientific computing(circular 
> convolution etc).
>
> If circular indexing is used then we don't need the arbitrary rule that -1 is 
> the index of the last element. Since -1 is the same as n-1 automatically in 
> modular arithmetic.
>
>
> A trivial objection:  "why not use list_name[i%n] whenever we need this 
> feature?" By the same token we could do away with negative indices and use 
> -1%n for example when we need to index with -1!
>
>
> Its unclear why that people have an irrational preference for indices that 
> lie to the left of 0 while strongly rejecting the idea of indices that lie to 
> the right of n-1!
>
> Python does not raise a "index out of bound" exception for negative indices 
> like other programming languages. If this negative indexing is a "feature" 
> (although it allows some fatal errors to slip) then indices above n-1 can 
> also be considered a feature!
>
> Are there any deep mathematical reasons for adopting  circular convention?
> Circular convolution is a most important operation in a wide variety of 
> scientific disciplines since the Discrete Fourier Transform (DFT) of the 
> circular convolution of two signals is the product of the transforms. Because 
> of the universal applicability of Fourier ideas in science and the close 
> mathematical relationship between finite length and periodic sequences 
> circular indexing is extensively used in signal processing and mathematics.
>
> We can extend the idea of circular indexing to multidimensional arrays. A 2D 
> array can be folded into a cylinder for indexing. Further this cylinder can 
> be folded into a toroid to reduce a triple FOR loop to a single FOR loop. A 
> deep mathematical justification for cylindrical indexing of 2D and in general 
> nD arrays is offered by the fact that n-dimensional DFT reduces n-dimensional 
> circular convolution to element-wise multiplication.
>
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/5TJYKFLBHB26WEFFQXMY6AGWS34XTIUR/
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
Antoine Rozo
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/64CU3BMLW5FSYD23NSIPUY2JEE6GDOAK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: async types?

2020-11-25 Thread Joao S. O. Bueno
A while back I could make a proof of concept of this with current Python,
no modifications needed.

The text is in portuguese -by I believe automatic translation should be
enough for the non-code
parts.

https://pt.stackoverflow.com/questions/390755/%c3%89-poss%c3%advel-definir-como-async-o-m%c3%a9todo-inicializador-de-uma-classe-em-python/390895#390895

Maybe it is worth putting that thing on a Pypi package?




On Wed, 25 Nov 2020 at 07:22, Ben Avrahami  wrote:

> Hi all, this is a general feeler for if this idea has any traction:
>
> All too often I see the following pattern in asyncio 3rd-party libs,
> either in their own source code or in the inusage:
> ```
> inst = SomeClass()
> await inst.initialize()
> ```
>
> What happens here is that, since coroutines cannot be used inside __init__
> methods, the __init__ method only stores the parameters, and another,
> asynchronous method actually initializes the object. This led many
> 3rd-party libs to use factory methods to create their class instances,
> which is unideal for both users and developers. We see this pattern in
> nearly any async 3rd-party lib I came across.
>
> To solve this, I propose a new metaclass: `AsyncType`. The core difference
> between AsyncType and type is that when called, AsyncType will produce a
> coroutine that asynchronously calls __new__ and __init__, and returns the
> instance, allowing simply for `instance = await SomeClass()`. In classes of
> `AsyncType`, the __new__ and __init__ methods must be async methods
> (returning coroutines).
>
> As an additional syntactic sugar, we could also have an `async
> class(base)` declaration that implicitly sets the class's metaclass to be
> AsyncType (or raise a TypeError if that is impossible).
>
> This proposal is obviously incomplete, and there are many open questions:
> what about __del__? How would one easily make an async ABC (without a
> metaclass conflict)? How would an async class easily inherit from a sync
> class (suppose sync class A implements __new__, and async class B(A)
> implements __init__)?  Perhaps `AsyncType` should only make the __init__
> method async and leave __new__ synchronous?
>
> I'd just like to get ahead of the (not unjustified) argument that
> constructors should be lightweight:
> 1. Python's __init__ is not strictly a constructor. It is, as the name
> implies, an initializer, that makes the type usable. Constructing the type
> without initializing it makes no sense and has no usages, so why seperate
> the operations?
> 2. We can see numerous examples of (syncronous) connect-on-initialize
> classes in the standard library, including ftplib, smtplib, and pretty much
> most *lib modules in the standard library "Internet Protocols and Support"
> doc page.
> 3. The "initialize in a separate method" pattern produces some
> strange-looking code where the class holds on to its initialization params
> even though it doesn't really need them after the separate methods. The
> classes should now also consider and safe-guard against cases where the
> separate method is called twice, or not at all.
>
> What does the community think about this idea?
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/DMTLZD6HLY6JTG2WY5RYSXFX3KLZQB46/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LBAJ3QC3V7GJUTITYD5HBTDSO7RUHKKT/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Getting rid of FOR loops and simplifying cicular conviolutions with Circular Indexing

2020-11-25 Thread Mathew M. Noel via Python-ideas
If circular indexing is used then instead of using a double FOR loop to go 
through a list M times we can iterate from 0 to M*N (where N is the length of 
the list) !!!


Almost all Machine Learning (ML) algorithms iterate for some predefined epochs 
over a large data-set. So a double FOR loop is extremely common in ML. Using 
circular indexing gets rid of this extra FOR loop. If we have to iterate 2 
times you can iterate using range(-n,n) but in most cases you need to iterate 
over 10 or more epochs in ML.


Most scientific applications of Python involve an outer FOR loop which 
progressively refines an approximation with an inner FOR loop by going through 
a list of items. So circular indexing is useful. In the following I discuss 
increasingly compelling reasons for adopting a circular indexing scheme in 
Python.


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. Consider a list 
with n elements: it would be perfectly reasonable to address the element 0 of 
the list using an index of n since n occurs after n-1 (if we assume that the 
list is bent into a circle). This feature can prove to be extremely useful. 
Consider the following example:


days_of_the_week = 
["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

It would be nice if

days_of_the_week[0]

is the same as

days_of_the_week[7]

is the same as

days_of_the_week[14] etc

In other words use modular indexing. In other words if the index is outside the 
range 0 to n-1, we simply take the remainder when the index is divided by n as 
the index.
Because of the close relationship between finite length sequences and periodic 
sequences this feature might simplify scientific computing(circular convolution 
etc).

If circular indexing is used then we don't need the arbitrary rule that -1 is 
the index of the last element. Since -1 is the same as n-1 automatically in 
modular arithmetic.


A trivial objection:  "why not use list_name[i%n] whenever we need this 
feature?" By the same token we could do away with negative indices and use -1%n 
for example when we need to index with -1!

Its unclear why that people have an irrational preference for indices that lie 
to the left of 0 while strongly rejecting the idea of indices that lie to the 
right of n-1!

Python does not raise a "index out of bound" exception for negative indices 
like other programming languages. If this negative indexing is a "feature" 
(although it allows some fatal errors to slip) then indices above n-1 can also 
be considered a feature!

Are there any deep mathematical reasons for adopting  circular convention?
Circular convolution is a most important operation in a wide variety of 
scientific disciplines since the Discrete Fourier Transform (DFT) of the 
circular convolution of two signals is the product of the transforms. Because 
of the universal applicability of Fourier ideas in science and the close 
mathematical relationship between finite length and periodic sequences circular 
indexing is extensively used in signal processing and mathematics.

We can extend the idea of circular indexing to multidimensional arrays. A 2D 
array can be folded into a cylinder for indexing. Further this cylinder can be 
folded into a toroid to reduce a triple FOR loop to a single FOR loop. A deep 
mathematical justification for cylindrical indexing of 2D and in general nD 
arrays is offered by the fact that n-dimensional DFT reduces n-dimensional 
circular convolution to element-wise multiplication.

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5TJYKFLBHB26WEFFQXMY6AGWS34XTIUR/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 out of bound exceptions to reduce the number of lines necessary for error checking. Although negative indexing is one of those quintessential pythonic traits, Python's indexing schemes do require more base and edge cases to consider in implementation. I feel the feature should be conserved in the language, not in practice.On Nov 24, 2020 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. Consider a list with n elements:
 it would be perfectly reasonable to address the element 0 of the list using an index of n since n occurs after n-1 (if we assume that the list is bent into a circle). This feature can prove to be extremely useful. Consider the following example:



days_of_the_week = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]It would be nice if days_of_the_week[0] is the same asdays_of_the_week[7] is the same asdays_of_the_week[14] etcIn other words use modular indexing.In other words if the index is outside the range 0 to n-1, we simply take the remainder when the index is divided by n as the index.Because of the close relationship between finite length sequences and periodic sequences this feature might simplify scientific computing(circular convolution etc).  




___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/J43OCZG6LBQHJCGWAYJR3V4EGR66K22A/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7P5J7JQ6XZNB5YV3AJORA25LIINMDQHE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] async types?

2020-11-25 Thread Ben Avrahami
Hi all, this is a general feeler for if this idea has any traction:

All too often I see the following pattern in asyncio 3rd-party libs, either
in their own source code or in the inusage:
```
inst = SomeClass()
await inst.initialize()
```

What happens here is that, since coroutines cannot be used inside __init__
methods, the __init__ method only stores the parameters, and another,
asynchronous method actually initializes the object. This led many
3rd-party libs to use factory methods to create their class instances,
which is unideal for both users and developers. We see this pattern in
nearly any async 3rd-party lib I came across.

To solve this, I propose a new metaclass: `AsyncType`. The core difference
between AsyncType and type is that when called, AsyncType will produce a
coroutine that asynchronously calls __new__ and __init__, and returns the
instance, allowing simply for `instance = await SomeClass()`. In classes of
`AsyncType`, the __new__ and __init__ methods must be async methods
(returning coroutines).

As an additional syntactic sugar, we could also have an `async class(base)`
declaration that implicitly sets the class's metaclass to be AsyncType (or
raise a TypeError if that is impossible).

This proposal is obviously incomplete, and there are many open questions:
what about __del__? How would one easily make an async ABC (without a
metaclass conflict)? How would an async class easily inherit from a sync
class (suppose sync class A implements __new__, and async class B(A)
implements __init__)?  Perhaps `AsyncType` should only make the __init__
method async and leave __new__ synchronous?

I'd just like to get ahead of the (not unjustified) argument that
constructors should be lightweight:
1. Python's __init__ is not strictly a constructor. It is, as the name
implies, an initializer, that makes the type usable. Constructing the type
without initializing it makes no sense and has no usages, so why seperate
the operations?
2. We can see numerous examples of (syncronous) connect-on-initialize
classes in the standard library, including ftplib, smtplib, and pretty much
most *lib modules in the standard library "Internet Protocols and Support"
doc page.
3. The "initialize in a separate method" pattern produces some
strange-looking code where the class holds on to its initialization params
even though it doesn't really need them after the separate methods. The
classes should now also consider and safe-guard against cases where the
separate method is called twice, or not at all.

What does the community think about this idea?
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DMTLZD6HLY6JTG2WY5RYSXFX3KLZQB46/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 
> list indexing. Thus element n corresponds to element 0, n+1 to element 
> 1 and so on. This has two consequences:
> 
> 
> 1.) Makes the Python convention of interpreting index -1 as the last 
> element more logical since -1 is the same as n-1 when taken modulo n. 

The convention is logical enough as it is. If doesn't need justification 
based on modular arithmetic.


> If -1 is the first element then to be logically consistent we have to 
> interpret index n as the element 0 as well!

No we don't. There is no logical consistency in arguing that index n 
**must** be interpreted as element 0. Don't mistake your *preference* 
for circular/modular indexing for *logical necessity*.

It is totally logically consistent to treat indexes as *linear* not 
circular, with zero-based indexing starting from the front of the 
sequence and negative indexes starting from the end.


> Obviously this does allow certain errors to slip

Indeed. That's a much stronger argument against your proposal than 
the weak argument from "logical consistency" for your proposal.


> but if you are going 
> to allow an index of -1 (indices that occur before 0) then you might 
> just as well allow indices above n-1 to be logically consistent.

That doesn't follow even a little bit. We're under no obligation to 
totally break a perfectly good, useful and logical model for indexing, 
changing it to a *worse* model, under a dubious and unjustified claim 
that we "might as well" change the model to be "logically consistent".

We don't add language features because we "might as well", we add them 
because they will improve the language.

The current model is already logically consistent. Moving to a circular 
model might be useful in a tiny fraction of cases, but will be worse for 
most cases.

But the fatal objection is that it will *break backwards compatibility* 
and therefore change the meaning of existing code. That rules out this 
change without a long period of deprecating the existing behaviour, 
followed by adding the new behaviour. And we don't do that just for a 
small benefit.

Two examples of how a circular model will break existing code (there 
may be more):

1. The "sequence protocol" for iteration.

The sequence protocol relies on calling `sequence[0]`, `sequence[1]`, 
and so forth, for increasing indices 0, 1, 2, 3, 4, ... until IndexError 
is raised. If we shift to a circular model, IndexError will not be 
raised, and sequences which are iterable today will turn into infinite 
loops under your proposal.

2. Slicing past the end index. Under the current design:

>>> a = "abcdefgh"
>>> a[3:10]
'defgh'

Under your proposal:

>>> a[3:10]  # proposed behaviour
'defghab'

Both of these are changes in behaviour, so would need to have a 
significant benefit over the status quo to justify breaking people's 
code.


> 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 !

Do you often need to go through a loop twice? Can you give an example?


> Although trivial for the case of one dimensional lists/arrays, 
> cylindrical indexing (for 2D arrays) and toroidal indexing (2D, 3D and 
> nD arrays) schemes might be worth exploring for n-dimensional NumPy 
> arrays.

I think you have the process backwards. We don't change the language 
because the proposal "might be worth exploring". Do your exploring 
first, and only if you find some really useful benefits of the proposal 
will we consider changing the language.

Python is not an experimental toy language with ten users. It is one of 
the most popular languages in the world, relied upon by tens or hundreds 
of thousands of people. We don't change existing behaviour and break 
people's code because it "might be useful".


> Circular and in general cylindrical or toroidal indexing schemes might 
> simplify coding by reducing the number of FOR loops when iterating 
> over nD arrays in NumPy.

Same again. *First* prove that it will do these things, then we consider 
whether the benefit is worth the disruption.

If you are claiming a benefit for numpy, have you asked numpy developers 
what they think of your proposal?


-- 
Steve
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KZ7LZZHXDG57ZFYU7XRPB7O5VQ6LQCTG/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 the
> 0th element. Consider a list with n elements: it would be perfectly
> reasonable to address the element 0 of the list using an index of n
> since n occurs after n-1 (if we assume that the list is bent into a
> circle). This feature can prove to be extremely useful.

No.

> Consider the
> following example:
> 
> 
> days_of_the_week =
> ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
> 
> It would be nice if
> 
> days_of_the_week[0]
> 
> is the same as
> 
> days_of_the_week[7]
> 
> is the same as
> 
> days_of_the_week[14] etc
> 
> In other words use modular indexing.

If you want to use modular indexing, do exactly that:

days_of_the_week[7 % 7]
days_of_the_week[14 % 7]
days_of_the_week[foo % 7]

As was already mentioned, you can even hide that behind a 'list'
subclass.
 

-- 
Best regards,
 Paul  mailto:pmis...@gmail.com
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/B2OO6Q7CCJDWLI2PHXCG3L4EISXTFQ6Y/
Code of Conduct: http://python.org/psf/codeofconduct/