LEGB

Python has names and references

Python object model

An assignment statement creates a symbolic name that you can use to reference an object. The statement x = 'foo' creates a symbolic name x that refers to the string object 'foo'.

LEGB

Namespace = declarative region that provides a scope to the identifiers. Collection of currently defined symbolic names along with information about the object that each name references.

  1. Local = locals() returns dict(), returns copy of local namespace
  2. Enclosing
    1. nonlocal a
  3. Global = globals() returns dict(), returns reference to object
    1. global a
  4. Built-In namespace = dir(__builtins__)

Examples

def fоo():
	return total + 1

total = 0
print(foo()) # 1 
dеf f(): 
    s = "I love London!"
    рrint(s) 

s = "I love Paris!" 
f()
рrint(s)

# I love London!
# I love Paris!
========================================================================
def f(): 
   print(s)
   s = "I love London!"
   print(s)
 
s = "I love Paris!"
f()
# UnboundLocalError: local variable 's' referenced before assignment
def nwe():
    b = (2, 4)
    print(locals()['b'])
    locals()['b'] += (4,)
    print(locals())
    print(b)

nwe()
# (2, 4)
# {'b': (2, 4)}
# (2, 4)

# WHY? locals() inside a function is a snapshot, not the real thing. 
# if you use += for tuple it will work, but different object will be created

dir(), vars()

  • Use dir(x) when you want a quick list of everything an object can do (including inherited methods, class attributes, and __slots__).
  • Use vars(x) when you need a dictionary of key-value pairs for an object's instance state (its actual stored data).
  • Use inspect module when you need deep metadata (e.g., source code, function signatures, or exact type hints).
  • Use hasattr() / getattr() when you want to dynamically check or access an attribute using a string variable.
  • vars()
    • Python objects usually store their instance variables in a dictionary that belongs to the object (except for slots). vars(x) returns this dictionary (as does x.__dict__)
    • if vars() is without argument then it returns locals()
    • vars(obj) simply retrieves obj.__dict__
  • dir()
    • dir(obj) implicitly calls obj.__dir__() if defined
    • dir(__builtins__)
    • dir returns slots, while vars doesn’t
    • displays class attributes as well
    • dir(x), returns a dictionary of x's "attributes, its class's attributes, and recursively the attributes of its class's base classes."

Resources

SuperMade with Super