[python] Scoping

Python now follow LEGB rule

LEGB Rule:

L. Local. (Names assigned in any way within a fuctnion (def or lambda), and not declared global in that function.

E. Enclosing function locals. (Name in the local scope of any and all enclosing functions (def or lambda), form 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,…

And it’s the same when meeting Modules.

Ex: the example will print ‘A’ instead of  ‘main’

main.py
------------------------------------

import A
owner = 'main'
a = A.show()
A.py
-----------------------------------------

owner='A'
def show()
    print owner
Advertisements