[Patches] [Patch #101138] 'for i indexing a in l': exposing the for-loop counter

noreply@sourceforge.net noreply@sourceforge.net
Thu, 17 Aug 2000 12:10:18 -0700


Patch #101138 has been updated. 

Project: 
Category: core (C code)
Status: Rejected
Summary: 'for i indexing a in l': exposing the for-loop counter

Follow-Ups:

Date: 2000-Aug-09 22:10
By: twouters

Comment:
This patch adds a way to get the loop counter in Python for-loops. The syntax is one Just quoted on python-dev today, which he attributes to Tim (and was probably proposed by half the thinking Python community by now ;)

It's a quick and dirty hack, but it works. There might be refcounting bugs and much more efficient ways to do it, but it works ;) It also lacks a test case and documentation.

-------------------------------------------------------

Date: 2000-Aug-11 07:27
By: hooft

Comment:
Do we need a new keyword for this functionality, or can we use zip?
Maybe adding zip-magic would help, but:

>>> a=['a','b','c','d'] 
>>> for i,el in zip(xrange(9999999),a):
...     print i,el
... 
0 a
1 b
2 c
3 d

-------------------------------------------------------

Date: 2000-Aug-11 07:33
By: hooft

Comment:
Or: 
>>> class indexing: 
...     def __init__(self,a):
...         self.data=a
...     def __getitem__(self,i):
...         return i,self.data[i]
... 
>>> for i,el in indexing(a):
...     print i,el
... 
0 a
1 b
2 c
3 d
>>> 

-------------------------------------------------------

Date: 2000-Aug-11 07:38
By: twouters

Comment:
The patch exposes the already-present loop counter to python code, rather than just adding a list of ranges to loop over. That is one of the reasons to make it a syntactic change: other techniques to use the builtin loop counter would require psychic interpreters ;)

The patch does *not* introduce a new keyword, by the way. the 'indexing' name is just a NAME, not a real keyword, and can be used as a variable just fine.

-------------------------------------------------------

Date: 2000-Aug-14 02:32
By: nowonder

Comment:
I think that using the builtin loop counter would be nice. But I don't see this justifying extra syntax. Paul Prescod's suggestion of adding .items() to lists seems more natural to me.
-------------------------------------------------------

Date: 2000-Aug-15 22:19
By: tim_one

Comment:
Assigned to Guido for Pronouncement, as this is the last chance he'll ever have to end the blood feud with his brother <wink>.
-------------------------------------------------------

Date: 2000-Aug-17 19:10
By: gvanrossum

Comment:
Don't like it.  Have to admit, in 'for i indexing a in l' I have no intuition about what i and a are!
-------------------------------------------------------

-------------------------------------------------------
For more info, visit:

http://sourceforge.net/patch/?func=detailpatch&patch_id=101138&group_id=5470