Reading-Notes

View the Project on GitHub

Game of Greed 2

Python Scope & the LEGB Rule:

Scopes overview

Scope types

  1. Global scopes: avalible for all code.
  2. Local scopes: available only within the scope.

You can create Python names through one of the following operations: [^1] |Operation |Statement | | ———– | ———– | |Assignments | x = value | |Import operations |import module/from module import name | |Function definitions |def my_func()| |Argument definitions in the context of functions |def my_func(arg1, arg2,… argN)| |Class definitions | class MyClass|

Note: There’s an important difference between assignment operations and reference or access operations. When you reference a name, you’re just retrieving its content or value. When you assign a name, you’re either creating that name or modifying it.

Python Scope vs Namespace

Using the LEGB Rule for Python Scope

The LEGB rule is a kind of name lookup procedure, which determines the order in which Python looks up names. For example, if you reference a given name, then Python will look that name up sequentially in the local, enclosing, global, and built-in scope. If the name exists, then you’ll get the first occurrence of it. Otherwise, you’ll get an error.

Differences between LEGB rules

More


Big O notation

More [^1]:https://realpython.com/python-scope-legb-rule/