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

  • SEARCH
  • Home
  • 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 8095949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:18:27+00:00 2026-06-05T21:18:27+00:00

Consider this (rather pointless) javascript code: function make_closure() { var x = 123, y

  • 0

Consider this (rather pointless) javascript code:

function make_closure() {
    var x = 123, y = 456;
    return function(varname) { return eval(varname) }
}

closure = make_closure()
closure("x") // 123
closure("y") // 456

The function returned from make_closure doesn’t contain any references to scope variables, but still is able to return their values when called.

Is there a way to do the same in python?

def make_closure():
    x = 123
    return lambda varname: ...no "x" here...

closure = make_closure()
print closure("x") # 123

In other words, how to write a closure that would “know” about variables in defining scope without mentioning them explicitly?

  • 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-05T21:18:28+00:00Added an answer on June 5, 2026 at 9:18 pm

    This is probably the closest thing to an exact equivalent:

    def make_closure():
      x = 123
      l = locals()
      return lambda(varname): eval(varname, None, l)
    
    closure = make_closure()
    print closure("x") # 123
    

    Ultimately, your problem isn’t that x wasn’t captured by your local closure, but that your local scope wasn’t passed into eval.

    See http://docs.python.org/library/functions.html#eval for details on the eval function.

    It probably goes without saying that this is not a very Pythonic thing to do. You almost never actually want to call eval in Python; if you think you do, step back and rethink your larger design. But when you do need to use it, you need to understand how it’s different from Javascript eval. It’s ultimately more powerful—it can be used to simulate dynamic scoping, or as a simplified version of exec, or to evaluate code in any arbitrarily-constructed environment—but that also means it’s trickier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0;
Consider this code snippet: class MyClass{ private List myList; //... public List getList(){ return
Consider this code: one.c: #include <stdio.h> int one() { printf(one!\n); return 1; } two.c:
This is rather interesting, I think. Consider following code, both the window.onload and body
Consider this code: new Ajax.Request('?service=example', { parameters : {tags : 'exceptions'}, onSuccess : this.dataReceived.bind(this)
Yet another argument with a friend of mine. Consider this code: class User <
Consider this: #ifndef GUARD_H #define GUARD_H ... #endif GUARD_H rather than: #ifndef GUARD_H #define
Consider this code: #include <iostream> int main() { int iTemp = 0; iTemp +=
Consider this code, struct A {}; struct B { B(const A&) {} }; void
Consider this as a challenge rather than its general approach. The reason I mention

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.