Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8262
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: writable iterators? |
| Date | 2011-06-22 21:50 -0700 |
| Organization | > Bestiaria Support Staff < |
| References | <ittfon$nku$1@dough.gmane.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.311.1308804643.1164.python-list@python.org> (permalink) |
On Wed, 22 Jun 2011 15:28:23 -0400, Neal Becker <ndbecker2@gmail.com>
declaimed the following in gmane.comp.python.general:
> for e in sequence:
> do something that reads e
> e = blah # will do nothing
>
> I believe this is not a limitation on the for loop, but a limitation on the
> python iterator concept. Is this correct?
Neither... It is a misunderstanding of what "=" assignment DOES.
e = blah
translates into "disconnect the NAME 'e' from whatever object it
currently refers to; connect the NAME 'e' to the same object that the
name 'blah" is connected to"
"for e in sequence" connects the name "e" to then next object in the
iterable. As long as your "writable" is a MUTABLE object, you CAN mutate
it...
>>> lol = [ [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9] ]
>>> for e in lol:
... e[1] = sum(e)
...
>>> lol
[[1, 6, 3], [4, 15, 6], [7, 24, 9]]
>>>
>>> for e in lol:
... e.reverse()
...
>>> lol
[[3, 6, 1], [6, 15, 4], [9, 24, 7]]
>>>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: writable iterators? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-06-22 21:50 -0700
csiph-web