What is Scope Resolution in Python?

Question

Explain with examples

in progress 0
Pragya Rani 55 years 2 Answers 1321 views Contributor 0

Answers ( 2 )

  1. Python Follows LEGB rule to look for variables:
    LEGB Rule:
    • L: Local — Names assigned in any way within a function (def or lambda),
    and not declared global in that function.
    • E: Enclosing function locals — Names in the local scope of any and all
    enclosing functions (def or lambda), from inner to outer.
    • G: Global (module) — Names assigned at the top-level Of a module file,
    or declared global in a def within the file.
    • B: Built-in (Python) — Names preassigned in the built-in names module :
    open, range, SyntaxError,…

  2. Scope resolution is way to find the place from which value of the variable come.
    Python scope resolution follow LEGB rule:
    L : Local ( values inside the function)
    E: enclosing (values inside the nested function)
    G: Global (values assigned at the top of the module)
    B: built in (value which is there inside the built in package of python

Leave an answer

Browse
Browse