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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:44:14+00:00 2026-05-13T10:44:14+00:00

I am translating some code from lisp to Python. In lisp, you can have

  • 0

I am translating some code from lisp to Python.

In lisp, you can have a let construct with the variables introduced declared as special and thus having dynamic scope. (See http://en.wikipedia.org/wiki/Dynamic_scope#Dynamic_scoping)

How can I do likewise in Python? It seems the language does not support this directly, if true, what would be a good way to emulate it?

  • 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-13T10:44:14+00:00Added an answer on May 13, 2026 at 10:44 am

    I feel Justice is plain right in his reasoning here.

    On the other hand — I can’t resist implementing proof of concept for still another programing paradigm “unnatural” to Python — I simply love doing this. 🙂

    So, I created a class whose objects’attributes are scopped just like you require (and can be created dynamically). As I said, it is just in a proof of concept state – but I think most usual errors, (like trying to access a variable ina scope it is not defined at all) should have errors raised, even if not the proper ones (IndexError due to a stack underflow instead of AttributeError, for example)

    import inspect
    
    
    class DynamicVars(object):
        def __init__(self):
            object.__setattr__(self, "variables", {})
    
        def normalize(self, stackframe):
            return [hash(tpl[0]) for tpl in stackframe[1:]]
    
        def __setattr__(self, attr, value):
            stack = self.normalize(inspect.stack())
            d = {"value": value, "stack": stack}
            if not attr in self.variables:
                self.variables[attr] = []
                self.variables[attr].append(d)
            else:
                our_value = self.variables[attr]
                if our_value[-1]["stack"] == stack:
                    our_value[-1]["value"] = value
                elif len(stack) <= len(our_value):
                    while our_value and stack !=  our_value["stack"]:
                        our_value.pop()
                    our_value.append(d)
                else: #len(stack) > len(our_value):
                    our_value.append(d)
        def __getattr__(self, attr):
            if not attr in self.variables:
                raise AttributeError
            stack = self.normalize(inspect.stack())
            while self.variables[attr]:
                our_stack = self.variables[attr][-1]["stack"]
                if our_stack == stack[-len(our_stack):]:
                    break
                self.variables[attr].pop()
            else:
                raise AttributeError
            return self.variables[attr][-1]["value"]
    
    
    # for testing:
    def c():
        D = DynamicVars()
        D.c = "old"
        print D.c
        def a():
            print D.c
        a()
        def b():
            D.c = "new"
            a()
        b()
        a()
        def c():
            D.c = "newest"
            a()
            b()
            a()
        c()
        a()
    
    c()
    

    2020 update – Another similar question showed up, and I crafted a hack that needs no special namespace objects (but which resorts to using inner things from cPython, like updating the locals() to actual variables: https://stackoverflow.com/a/61015579/108205 (works with Python 3.8)

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

Sidebar

Related Questions

I'm translating some code from MATLAB to Python and I'm stuck with the corrmtx()
I'm teaching myself Python and I was translating some sample code into this class
I'm translating some code from the DataContractSerializer to the impressive protobuf-net serializer, and one
I am translating some code from VB to C# and came across this: Format(seg.R,
I need help with translating some code from VB to C#. Public Function ToBase36(ByVal
I'm actually translating some C++ code (which I know very little about, and have
friends, I have a piece of code which can perform simple add, subtraction, multiplication,
Hey folks, I have the following piece of code from C++. for (int i=0;
I'm testing some code in C# from Visual Studio Express 2008: delegate void Hm(int
I am looking at some PHP code where there are translation strings. For French

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.