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?
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)
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)