[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

when is filter test applied?

I'm not certain that it isn't behaving as expected - my code is quite

On Tue, Oct 3, 2017 at 11:35 AM Paul Moore <p.f.moore at gmail.com> wrote:

> My intuition is that the lambda creates a closure that captures the
> value of some_seq. If that value is mutable, and "modify some_seq"
> means "mutate the value", then I'd expect each element of seq to be
> tested against the value of some_seq that is current at the time the
> test occurs, i.e. when the entry is generated from the filter.
> You say that doesn't happen, so my intuition (and yours) seems to be
> wrong. Can you provide a reproducible test case? I'd be inclined to
> run that through dis.dis to see what bytecode was produced.
> Paul
> On 3 October 2017 at 16:08, Neal Becker <ndbecker2 at gmail.com> wrote:
> > In the following code (python3):
> >
> > for rb in filter (lambda b : b in some_seq, seq):
> >   ... some code that might modify some_seq
> >
> > I'm assuming that the test 'b in some_seq' is applied late, at the start
> of
> > each iteration (but it doesn't seem to be working that way in my real
> code),
> > so that if 'some_seq' is modified during a previous iteration the test is
> > correctly performed on the latest version of 'some_seq' at the start of
> each
> > iteration.  Is this correct, and is this guaranteed?
> >
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list