How can one create new scopes in python

后端 未结 12 1184
渐次进展
渐次进展 2020-12-10 01:08

In many languages (and places) there is a nice practice of creating local scopes by creating a block like this.

void foo()
{
     ... Do some stuff ...

             


        
相关标签:
12条回答
  • 2020-12-10 01:30

    A scope is a textual region of a Python program where a namespace is directly accessible. “Directly accessible” here means that an unqualified reference to a name attempts to find the name in the namespace...

    Please, read the documentation and clarify your question.

    btw, you don't need if(TRUE){} in C, a simple {} is sufficient.

    0 讨论(0)
  • 2020-12-10 01:41

    In Python, scoping is of three types : global, local and class. You can create specialized 'scope' dictionaries to pass to exec / eval(). In addition you can use nested scopes (defining a function within another). I found these to be sufficient in all my code.

    As Douglas Leeder said already, the main reason to use it in other languages is variable scoping and that doesn't really happen in Python. In addition, Python is the most readable language I have ever used. It would go against the grain of readability to do something like if-true tricks (Which you say you want to avoid). In that case, I think the best bet is to refactor your code into multiple functions, or use a single scope. I think that the available scopes in Python are sufficient to cover every eventuality, so local scoping shouldn't really be necessary.

    I hope this helps.

    0 讨论(0)
  • 2020-12-10 01:42

    variables in list comprehension (Python 3+) and generators are local:

    >>> i = 0
    >>> [i+1 for i in range(10)]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> i
    0
    

    but why exactly do you need this?

    0 讨论(0)
  • 2020-12-10 01:42

    update

    This answer is wrong. with statement doesn't restrict the scope; it only allows you to clean up when exiting from the scope (though you can make the object throw exception once accessed after exiting).


    Depending on your case, you might want with statement:

    with Cat() as cat, Cake() as cake:
        cat.eat(cake)
    

    is equivalent to

    # scope begin
    cat = Cat()
    cake = Cake()
    cat.eat(cake)
    # scope end
    
    0 讨论(0)
  • 2020-12-10 01:47

    I would see this as a clear sign that it's time to create a new function and refactor the code. I can see no reason to create a new scope like that. Any reason in mind?

    0 讨论(0)
  • 2020-12-10 01:47

    As others have suggested, the python way to execute code without polluting the enclosing namespace is to put it in a class or function. This presents a slight and usually harmless problem: defining the function puts its name in the enclosing namespace. If this causes harm to you, you can name your function using Python's conventional temporary variable "_":

    def _():
        polluting_variable = foo()
        ...
    _() # Run the code before something overwrites the variable.
    

    This can be done recursively as each local definition masks the definition from the enclosing scope.

    This sort of thing should only be needed in very specific circumstances. An example where it is useful is when using Databricks' %run magic, which executes the contents of another notebook in the current notebook's global scope. Wrapping the child notebook's commands in temporary functions prevents them from polluting the global namespace.

    0 讨论(0)
提交回复
热议问题