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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:41:20+00:00 2026-05-12T16:41:20+00:00

I wonder if there is a good way to bind local variables in python.

  • 0

I wonder if there is a good way to bind local variables in python. Most of my work involves cobbling together short data or text processing scripts with a series of expressions (when python permits), so defining object classes (to use as namespaces) and instantiating them seems a bit much.

So what I had in mind was something like in (common) lisp, where you could do something like

(setq data '(1 2 3))
(setq output 
      (let ( (x (nth 2 data)) )
       x + x))

In python, the best I could come up with is

data = [1,2,3]
output = ((lambda x: x + x)
          (data[2]))

These are, of course, very simple examples but might there be something that is as scalable as let or let* in lisp? Are defining classes the best way to go to create a local namespace?…(but feels a little less interactive that way)

Edit: So to further explain the intention (my apologies for vagueness), I want to reduce the use of global variables. So in the case above, I meant to use the extraction operator as a general case of any type of operation that might not want to be repeated. For instance, one might write either

output = data[2] + data[2]

or

x = data[2]
output = x + x
del x

to accomplish the same result. In essence, if the desired operation on ‘data’ is more complicated then getting the second item, I wouldn’t want to type it out multiple times, or let the computer compute the value of the same expression more times than necessary. So in most cases one would assign the result of the operation, in this case, data[2], or operator.itemgetter(2)(data), to some variable in the global space, but I have an aversion to leaving variables around in the global space if they were only necessary to store intermediate values in a computation… hence the use of the ‘del’ command immediately afterwards. Defining a local environment or namespace and binding intermediate results to local variables would be an ideal alternative.

  • 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-05-12T16:41:20+00:00Added an answer on May 12, 2026 at 4:41 pm

    I can only second Lennart and Daniel – Python is not Lisp, and trying to write language X into language Y is usually inefficient and frustrating at best.

    First point: your example code

    data = [1,2,3]
    output = ((lambda x: x + x)
              (data[2]))
    

    would be much more readable as:

    data = [1, 2, 3]
    output = (lambda x=data[2] : x +x)()
    

    but anyway, in this concrete case, using a lambda is total overkill, overcomplexificated, and mostly inefficient. A braindead

    output = data[2] + data[2]
    

    would JustWork(tm) !-)

    Now wrt/ to local bindings / namespaces, the usual solution is to use… functions – eventually nested. While 100% object (as in “everything is an object”), Python is not pure object, and plain functions are just fine. FWIW, even for “scripts”, you should put your logic in a function then call it – function’s local namespace access is faster than “global” (really: module level) namespace access. The canonical pattern is

    import whatever
    
    def some_func(args):
        code_here
    
    def some_other_func(args)
        code_here
    
    def main(args):
        parse_args
        some_func(something)
        some_other_func(something_else)
        return some_exit_code
    
    if __name__ == '__main__'
        import sys
        sys.exit(main(sys.argv))         
    

    Note also that nested functions can also access the enclosing namespace, ie

    def main():
        data = [1, 2, 3]
        def foo():
           x = data[2]
           return x + x
        print foo()
        data = [4, 5, 6]
        print foo()
        # if you want the nested function to close over its arguments:
        def bar(data=data):
           x = data[2]
           return x + x
        print bar()
        data = [7, 8, 9]
        print bar()
    

    HTH

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

Sidebar

Related Questions

I wonder if there is a way to either programatically or using a third
I wonder if there's a way to do the following: I have a structure
I wonder if there is a way to create a Custom List in Sharepoint,
i wonder if there is a way to access a control's templatepart from within
I wonder if there is a way to set the value of #define in
I wonder if there is a less verbose way to do Input Verification in
i wonder if there is a way to generate valid GUIDs/UUIDs where the first
I wonder if there is a way to use ungreedy matching in JavaScript? I
Is there a good way to map vectors? Here's an example of what I
I wonder if there is something like a standalone version of Visual Studio's "Immediate

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.