Sobes.tech
Junior — Middle

How was the variable and function access zone organized in your previous project?

sobes.tech AI

Answer from AI

In Python, the scope of variables and functions is determined by the LEGB rules (Local, Enclosing, Global, Built-in):

  • Local — variables declared inside a function or method.
  • Enclosing — variables from the outer (enclosing) functions, if the function is nested.
  • Global — variables declared at the module level.
  • Built-in — names of Python's built-in functions and variables.

When referencing a name, Python searches in this order. To modify the scope inside a function, you can use the keywords global (to modify a global variable) and nonlocal (to modify a variable from an enclosing function).

Example:

x = 10  # global variable

def outer():
    y = 5  # enclosing
    def inner():
        nonlocal y
        y += 1
        print(y)  # access to enclosing
    inner()
    print(y)

outer()
print(x)