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


Groups > comp.lang.python > #60771

Re: For-each behavior while modifying a collection

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: For-each behavior while modifying a collection
Date 2013-11-29 11:37 -0500
Organization IISS Elusive Unicorn
References <mailman.3360.1385653859.18130.python-list@python.org> <52980885$0$29993$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.3401.1385743058.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 29 Nov 2013 03:22:45 GMT, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> declaimed the following:


>Even if there was a counter, how should it be modified? The code you show 
>was this:
>
>def keepByValue(self, key=None, value=[]):
>    for row in self.flows:
>        if not row[key] in value:
>            self.flows.remove(row)
>
>
>What exactly does the remove() method do? How do you know?
>
>self.flows could be *any object at all*, it won't be known until run-
>time. The remove method could do *anything*, that won't be known until 
>runtime either. Just because you, the programmer, expects that self.flows 
>will be a list, and that remove() will remove at most one item, doesn't 
>mean that Python can possibly know that. Perhaps self.flows returns an 
>subclass of list, and remove() will remove all of the matching items, not 
>just one. Perhaps it is some other object, and rather than removing 
>anything, in fact it actually inserts extra items in the middle of the 
>sequence. (There is no law that says that methods must do what they say 
>they do.)
>

	Let's really confuse matters...

	Say "self.flows" is something derived from a database cursor/result
set...

	Then "self.flows.remove(row)" could: 1) remove the row from the
cursor/result set [iterating over a cursor tends, in my experience, to use
up the items]; 2) execute a query to remove the matching row from the
database itself; 3) do both...
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

For-each behavior while modifying a collection Valentin Zahnd <v.zahnd@gmail.com> - 2013-11-28 16:49 +0100
  Re: For-each behavior while modifying a collection Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-29 03:22 +0000
    Re: For-each behavior while modifying a collection Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-29 11:37 -0500

csiph-web