Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8262 > unrolled thread

Re: writable iterators?

Started byDennis Lee Bieber <wlfraed@ix.netcom.com>
First post2011-06-22 21:50 -0700
Last post2011-06-22 21:50 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: writable iterators? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-06-22 21:50 -0700

#8262 — Re: writable iterators?

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2011-06-22 21:50 -0700
SubjectRe: writable iterators?
Message-ID<mailman.311.1308804643.1164.python-list@python.org>
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/

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web