ПРАВИЛО LEGB PYTHON

Principles of LEGB in Python:

LEGB stands for Local, Enclosing, Global, Built-in and it specifies the order of namespace searching in Python. This is important to understand how Python resolves names and variables.

The search happens in the following order:

L - Local namespace, which includes any local names defined in the current function

E - Enclosed namespace, which includes any names defined in the local scope of any enclosing functions, from inner to outer

G - Global namespace, which includes any names defined at the top level of a module or declared global within a function

B - Built-in namespace, which includes any built-in functions or modules

For example, if we have a variable 'x' used in a function, Python will search for it in the following order: local, enclosing, global, built-in namespaces, stopping as soon as it's found.

x = 5
def example():
    x = 10
    print(x)
example() # Output: 10

In the above example, when we call the function 'example', Python will first search for the variable 'x' in the local namespace of the function, and find the value 10, so it will print 10.

LEGB Rule in Python

Python for Testers #29 -Variable Scope in Python - LEGB Rule in Python

What is Python scope resolution? 🔬

Собеседование Python 2023. Разбор базовых вопросов

Python Tutorial: Variable Scope - Understanding the LEGB rule and global/nonlocal statements

Scopes in Python tutorial - How scoping works in Python - LEGB rule explained

Python Tutorials - LEGB Rule - Local And Global Scope - global and non local Keyword

Python Scope - Varieables Globales y Locales 💾 # 011-2

BLGPG-B01BF6F89910-24-11-23-22

Новые материалы: