A context manager could be used for this. On exit it should delete the variables created inside it. Someting like this:

class Scope:
    def __enter__(self):
        self._globals = list(globals().keys())
        return self
    def __exit__(self, exc_type, exc_value, traceback):
        del_vars = [var for var in globals()
                    if var not in self._globals]
        for var in del_vars:
            del globals()[var]

Then one can write:

with Scope():
    a = 1
    with Scope():
        b = 2
    # no more b here
# no more a or b here

Maybe such an (optimized) context manager could be added to Python?


Op 29/11/2022 om 02:49 schreef Anony Mous:
As it stands now, to create a local scope, you must define a function.

However, there are many cases where various things can reasonably be done inline. Preparatory code is often done this way.

Good coding practice is generally accepted to be that variables are local if at all possible. However, in direct, inline Python code, we're inherently creating variables with a global scope.

We don't actually need a function for this kind of code; but a local scope would often be valuable (as we see with lambda.) Moving things off into a function can create a circumstance where we have to go looking for the function. When something is a "one-shot" as in global preparatory code, that's doing more work, and creating more indirection, than needs actually be done. But global operations have their own pitfalls, one of which is variable collisions. So Python sort of drives us to make "one-use" functions where lambdas are insufficient to the case in order to control locality.

You can end up writing things like...

def PrepOne():
     for MyCount in range(0,10):
         pass

PrepOne()

...which achieves the locality desired for variable MyCount, but is clunky. It also clutters the global namespace with prepOne, which has no good reason to exist.

So I'm thinking of something along these lines:

local:    # <== or whatever syntax makes sense
     for MyCount in range(0,10):
         pass

So in that example, the code is inline - runs as part of the global, sequential code execution - but the variable MyCount is local and goes "poof" once the local: scope is exited.

This is the scoping equivalent of a pair of curly braces in c and c++. I would like to see it be nestable, as scopes are in c/c++:

local:    # <== or whatever syntax makes sense
     for MyCount in range(0,10):
         local:
             for YourCount in range(0,MyCount+1)
                 pass

There, MyCount is available in both scopes (the first local: is an enclosing scope) and YourCount is available only in the second local: scope.

Or, local: could be very strict, and require further syntax to incorporate a previous scope, something like this:

local:    # <== or whatever syntax makes sense
     for MyCount in range(0,10):
         local:
             incorporate MyCount # <== or whatever syntax makes sense
             for YourCount in range(0,MyCount+1)
                 pass

Lastly, I suggest that the global keyword be used to expose global scope variables, just as it is within def functions:

TheirCount = 10
local:    # <== or whatever syntax makes sense
     global TheirCount
     for MyCount in range(0,TheirCount):
         local:
             for YourCount in range(0,TheirCount)
                 pass

This would also be useful within normal functions, and again allows the program flow to be linear rather than calling a function-within-a-function.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/3ZEM5FPYWI24G3EOM3HA2I4RKWCMR3J5/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5L3GLLLFEEB5Z722XQUCKPGSUJWAFNDY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to