Scope and LEGB Rule Explained - Python Tutorial

Опубликовано: 23 Октябрь 2019
на канале: DMK Data
1,042
32

The LEGB rule is central to the way Python works. Whenever we call a name, the LEGB rule is used to find the value (or rather object) attached to that name. LEGB stands for Local, Enclosing, Global and Builtin. This is the order in which Python searches for name. For an expression in a local namespace, Python goes looking for the assignment of the name in that local namespace first. It goes upward from the expression and when it reached the end of the local namespace, it goes to the enclosing, then the global and finally the builtin namespace.

Within a script/module, only functions, classes and list comprehensions (only in Python 3!) create their own local scope. When an expression is called in the global namespace, Python never looks in the local or enclosing namespaces. The LEGB rule only goes in one direction. Therefore, local name assignments do not affect global assignments.

Feel free to leave a comment if you have any questions or feedback.

Python:
https://www.python.org/


Смотрите видео Scope and LEGB Rule Explained - Python Tutorial онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь DMK Data 23 Октябрь 2019, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 1,042 раз и оно понравилось 32 людям.