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


Groups > comp.lang.python > #99555

Re: What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?))

From Nobody <nobody@nowhere.invalid>
Newsgroups comp.lang.python
Subject Re: What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?))
Date 2015-11-26 11:13 +0000
Organization A noiseless patient Spider
Message-ID <pan.2015.11.26.11.13.43.440000@nowhere.invalid> (permalink)
References <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> <877fldnm9z.fsf@handshake.de> <mailman.73.1448459485.20593.python-list@python.org>

Show all headers | View raw


On Wed, 25 Nov 2015 14:51:23 +0100, Antoon Pardon wrote:

> Am I missing something?

The issue is with lambdas rather than with list comprehensions per se.

Python's lambdas capture free variables by reference, not value.

	> x = 3
	> f = lambda y: x + y
	> f(0)
	3
	> x = 7
	> f(0)
	7

The same issue applies to nested functions:

	> def foo():
	=    x = 3
	=    def f(y):
	=       return x + y
	=    print f(0)
	=    x = 7
	=    print f(0)
	= 
	> foo()
	3
	7

And also to non-nested functions (but most people expect that):

	> x = 3
	> def f(y,x=x):
	=   return x + y
	=
	> print f(0)
	3
	> x=7
	> print f(0)
	3

If you want to capture a variable by value, add a parameter with a default
value using that variable:

	> def foo():
	=    x = 3
	=    def f(y, x=x):
	=       return x + y
	=    print f(0)
	=    x = 7
	=    print f(0)
	= 
	> foo()
	3
	3

This also works for lambdas:

	> x = 3
	> f = lambda y,x=x: x + y
	> f(0)
	3
	> x = 7
	> f(0)
	3

Returning to the original expression:

	> q = [lambda x: i * x for i in range(4)]
	> q[0](1), q[3](1)
	(3, 3)
	> q = [lambda x,i=i: i * x for i in range(4)]
	> q[0](1), q[3](1)
	(0, 3)

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


Thread

What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?)) Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-25 14:51 +0100
  Re: What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?)) Nobody <nobody@nowhere.invalid> - 2015-11-26 11:13 +0000
    Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 12:52 +0100
      Re: What does a list comprehension do Marko Rauhamaa <marko@pacujo.net> - 2015-11-26 14:56 +0200
        Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 14:33 +0100
          Re: What does a list comprehension do Marko Rauhamaa <marko@pacujo.net> - 2015-11-26 15:56 +0200
            Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 15:26 +0100
              Re: What does a list comprehension do Marko Rauhamaa <marko@pacujo.net> - 2015-11-26 17:36 +0200
                Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 20:18 +0100
              Re: What does a list comprehension do Jussi Piitulainen <harvest@is.invalid> - 2015-11-26 18:11 +0200

csiph-web