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 9185311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:14:52+00:00 2026-06-17T19:14:52+00:00

l = [1, 2, 3] a,b,c = [lambda: n*n for n in l] a()

  • 0
l = [1, 2, 3]
a,b,c = [lambda: n*n for n in l]
a()  #=> 9
b()  #=> 9
c()  #=> 9

Why is this? I expected a(), b(), c(), to be 1, 4, and 9.

  • 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-17T19:14:53+00:00Added an answer on June 17, 2026 at 7:14 pm

    n isn’t in a local closure of the functions.

    try

    a, b, c = [lambda n=n: n*n for n in l]
    

    This “abuse” of the default parameter causes a local variable called n to be created for each function

    Here is another way to create a closure in Python2

    >>> L=[1, 2, 3]
    >>> def fgen():
    ...     local_n = global_n
    ...     def f():
    ...         return local_n * local_n
    ...     return f
    >>> a, b, c = [fgen() for global_n in L]
    >>> a()
    1
    >>> b()
    4
    >>> c()
    9
    

    It won’t work in Python3 though because the loop var in the list comprehension isn’t leaked into the global scope

    Python3 does a better job of preventing us using a global in the function, so you need to pass a parameter if you want to use a list comprehension

    >>> L=[1, 2, 3]
    >>> def fgen(param_n):
    ...     local_n = param_n
    ...     def f():
    ...         return local_n * local_n
    ...     return f
    ... 
    >>> a, b, c = [fgen(n) for n in L]
    >>> a()
    1
    >>> b()
    4
    >>> c()
    9
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code works as expected (does nothing, even doesn't produce warning/errors): l = lambda
What is this 'Lambda' everyone keeps speaking of? A lot of people seem to
I am very confused. I have this lambda expression: tvPatientPrecriptionsEntities.Sort((p1, p2) => p1.MedicationStartDate .Value
I've been really digging into LINQ, and I'm trying to hash out this lambda
To help me better understand lambda I wrote this short snippet that rotates and
This code works fine in C#: Expression.Lambda(LambdaBody); But none of the methods for building
So this pattern: def foo(&block) block.call end foo lambda { puts 'hi' } Is
I have model like this: scope :search_posts, lambda { |query| mega_posts.where( title LIKE ?
I am wondering whether I can use the this keyword inside a C# lambda,
I have used this function to convert string to bits. def a2bits(chars): return bin(reduce(lambda

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.