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

noreply@sourceforge.net noreply@sourceforge.net
Fri, 11 Aug 2000 00:38:45 -0700


Patch #101138 has been updated. 

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

Follow-Ups:

Date: 2000-Aug-09 15: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 00: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 00: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 00: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.

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

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

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