x[i] as loop variable

G

Gerrit Holl

Hi,

PEP 289 says:
(Loop variables may also use constructs like x 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.
 
R

Raymond Hettinger

[Gerrit Holl]
PEP 289 says:
(Loop variables may also use constructs like x 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
 
J

Just

"Raymond Hettinger said:
[Gerrit Holl]
PEP 289 says:
(Loop variables may also use constructs like x 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
 
R

Raymond Hettinger

[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
 
E

Erik Max Francis

Raymond said:
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.
 
E

Erik Max Francis

Just said:
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 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.
 
A

Alex Martelli

Raymond said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top