473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

x[i] as loop variable

Hi,

PEP 289 says:
(Loop variables may also use constructs like x[i] or x.a; this form may be deprecated.)

What is this about? I found this:
19:41:33:232:35 >>> a=Test()
19:41:37:232:36 >>> for a.foo in L: print a.foo
19:41:37:232:36 ...
<__main__.Test object at 0x4018516c>
<__main__.Test object at 0x401854cc>
<__main__.Test object at 0x401854ac>
19:41:49:232:37 >>> for obj in L: print obj.foo,
19:41:49:232:37 ...
bar baz bax

What is the use of this? When should I use it? Why 'may it be deprecated'?
I don't see this construct in the Language Reference. I looked at:
http://www.python.org/dev/doc/devel/ref/for.html

yours probably,
Gerrit.

--
that he caught run away from him, then shall he swear
to the owners of the slave, and he is free of all blame.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
http://www.sp.nl/

Jul 18 '05 #1
6 1424
[Gerrit Holl]
PEP 289 says:
(Loop variables may also use constructs like x[i] or x.a; this form may be deprecated.)

That was taken out of the pep because it is a distractor issue.

What is the use of this? When should I use it?


In general, it should never be used unless you're trying to obfuscate
your code. It is a long standing, unintended by-product of the
grammar that any lvalue can be used as a loop variable. Most folks
aren't aware of it and everyone is better off not knowing about it.
In fact, I wish it hadn't come-up at all because someone will read
about it and adopt the atrocity as their new favorite style.

So, close your eyes, forget this note, and go read about
something interesting and useful.
Raymond Hettinger

Jul 18 '05 #2
In article <yR******************@nwrdny01.gnilink.net>,
"Raymond Hettinger" <vz******@verizon.net> wrote:
[Gerrit Holl]
PEP 289 says:
(Loop variables may also use constructs like x[i] or x.a; this form may be

deprecated.)

That was taken out of the pep because it is a distractor issue.

What is the use of this? When should I use it?


In general, it should never be used unless you're trying to obfuscate
your code. It is a long standing, unintended by-product of the
grammar that any lvalue can be used as a loop variable. Most folks
aren't aware of it and everyone is better off not knowing about it.
In fact, I wish it hadn't come-up at all because someone will read
about it and adopt the atrocity as their new favorite style.


But isn't the feature a neccesity to make this work:

for a, b in zip(seqA, seqB):
...

?

Just
Jul 18 '05 #3
[Just]
But isn't the feature a neccesity to make this work:

for a, b in zip(seqA, seqB):

Yes, that is why it is there to begin with.
But that doesn't mean going off the deep-end and writing:

for a[3] in data: . . .
for a[:] in data: . . .
for a.abomination in data: . . .
for d[k] in data: . . .
Raymond Hettinger
Jul 18 '05 #4
Raymond Hettinger wrote:
In general, it should never be used unless you're trying to obfuscate
your code. It is a long standing, unintended by-product of the
grammar that any lvalue can be used as a loop variable. Most folks
aren't aware of it and everyone is better off not knowing about it.
In fact, I wish it hadn't come-up at all because someone will read
about it and adopt the atrocity as their new favorite style.


Yes, I encountered this when implementing iterative control markups for
EmPy. I was quite surprised that such syntaxes were allowed in Python;
I'd never seen them or heard of them being used, and only through an
experiment did I realize much to my surprise that, as you say, any
lvalue can be used as the iteration variable.

Even though the EmPy control markups are intended to mimic the native
Python control structures as closely as possible (the pinciple of least
surprise), needless to say, I got no complaints about deliberately
deviating from Python in this respect; in EmPy, iteration variables must
be simple variable names and nothing more.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Pick the roses from the thorns / Wash with waters of the storms
\__/ Chante Moore
Jul 18 '05 #5
Just wrote:
But isn't the feature a neccesity to make this work:

for a, b in zip(seqA, seqB):
...

?


I would guess that's _why_ the "feature" is there, but you can certainly
have one without the other. To use the EmPy control markup example
again (which is, as I mentioned in another post, intended to mimic the
native Python control structures as closely as is reasonably possible),
I support tuple iteration variables (e.g., @[for x, y in seq]) -- even
hierarchically -- but not the more generalized lvalues (@[for a[i] in
seq] or @[for a.x in seq] are errors).

I would submit that in Python, the latter examples came along as excess
baggage for the purpose for supporting tuple variables (which is indeed
a very useful feature, and thus is supported in EmPy). I can't imagine
anyone actually using such things deliberately except to be difficult.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Pick the roses from the thorns / Wash with waters of the storms
\__/ Chante Moore
Jul 18 '05 #6
Raymond Hettinger wrote:
But that doesn't mean going off the deep-end and writing:

for a[3] in data: . . .
for a[:] in data: . . .
for a.abomination in data: . . .
for d[k] in data: . . .


Still, there IS some "coolness factor" in:
d = {}
for k, d[k] in pairs: pass .... d

{'p': 'i', 'k': 'o', 'b': 'a', 'm': 'a', 'l': 'u'}

after all, "for k, d[k] in pairs: pass" is so much more
show-offy than a mere "d.update(dict(pairs))"...!-)
Alex

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: beliavsky | last post by:
In a Python 'for' loop, one can change the value of looping variable, so that for i in range(3): i = i*5 print i is legal code In Fortran 90 and 95, the analogous code
33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
12
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
7
by: Mahesh Kumar Reddy.R | last post by:
Hi Can any body resolve this.. In what cases one of the loop constructs better than other interms of speed , space and any other (redability). thanks mahesh
6
by: david | last post by:
I try to use "for" loop to retrieve and assign values in web form. The code is in the following. But it can not be compiled. What I want to do is: txtQ1.Text =...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
14
by: Michael Moreno | last post by:
Hello, Would you know what is best practice please between say: CODE 1: TimeSpan ts; for (i=0; i<1000; i++) {
4
by: =?Utf-8?B?VG9kZCBKYXNwZXJz?= | last post by:
Hey guys, Is there ANY way to accomplish this: (see below)? Basically, I want to have a loop (a < 3 is just for testing purposes, it will be an underermined amount). In this loop, I want to be...
5
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
1
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.