Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8513619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:37:31+00:00 2026-06-11T04:37:31+00:00

In this code, the x in the lambda refers to the x in the

  • 0

In this code, the x in the lambda refers to the x in the for statement. So y[0]() returns 2:

x = 0
y = [lambda : x for x in range(3)]
y[0]()

But in this code, the x in the lambda refers to global x, so x[0]() returns global x itself:

x = [lambda : x for x in range(3)]
x[0]()

I want to know why the x in the lambda refers to the local x in the first piece of code but global x in the second piece of code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:37:33+00:00Added an answer on June 11, 2026 at 4:37 am

    x refers to the global x in both pieces of code. Indeed, there is nothing but a global x in both pieces of code. There are no local variables here, only global variables.

    In the first example, the global value of x is 2, because that was the last value assigned to it by the list comprehension. List comprehensions leak their variables into the enclosing scope as described by @wim. Since the enclosing scope here is the global scope, the variable x is leaked into global scope, overwriting the value 0 that you set earlier.

    In the second example, you create the list comprehension, but then assign its value to the (global) variable x. This overwrites whatever was already in x, so the value of the global variable x is now the list.

    In both cases, when you call one of the functions in the list (any one!), it returns the current value of x. You can see this here:

    >>> y = [lambda : x for x in range(3)]
    >>> y[0]()
    2
    >>> x = 88
    >>> y[0]()
    88
    >>> x = [lambda : x for x in range(3)]
    >>> y = x
    >>> y[0]()
    [<function <lambda> at 0x017789B0>,
     <function <lambda> at 0x01828DB0>,
     <function <lambda> at 0x01828F30>]
    >>> x = 88
    >>> y[0]()
    88
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code works fine in C#: Expression.Lambda(LambdaBody); But none of the methods for building
This code : http://jsfiddle.net/bYtTG/1/ Works as I want in FF, chrome and opera, but
This code is not pretty, but I want to print out the last generated
Hi Experts I want to use Lambda Expression and write this code: Func<Regions, SelectAllRegion>
I've got this snippet of code: (define-key lisp-interaction-mode-map (kbd C-c C-e) (lambda () (let
For example, take a look at this code (from tspl4): (define proc1 (lambda (x
I'm trying to switch this linq code to a lambda expression: foreach (var id
I have this code from the PIL-handbook but I get an error message. from
I have this code and I would like to know if I can change
I've spent hours trying to debug this code. I want to get the second-to-last

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.