.. include:: global.rst =============================== Names and Environment in Python =============================== Overview ======== In Python, *names* refer to identifiers that are bound to objects, and the *environment* refers to the mapping between names and the objects they reference. Understanding how names are created, looked up, and managed is essential for writing correct and efficient Python programs. -------------------------------------------------- Names (Identifiers) =================== - A *name* is a label used to reference an object. - Created through assignment. Example: x = 10 - Here, "x" is a name bound to the integer object 10. Rules: - Must start with a letter or underscore (_) - Can contain letters, digits, and underscores - Case-sensitive -------------------------------------------------- Binding ======= - Binding is the association between a name and an object. Example: a = [1, 2, 3] b = a - Both "a" and "b" refer to the same list object. Key Idea: - Python uses *reference semantics*, not copying by default. -------------------------------------------------- Namespaces ========== A *namespace* is a container that maps names to objects. Types of Namespaces: -------------------- 1. Local Namespace - Inside a function 2. Enclosing Namespace - In nested functions 3. Global Namespace - At the module level 4. Built-in Namespace - Predefined names (e.g., len, print) -------------------------------------------------- LEGB Rule ========= Python resolves names using the LEGB rule: - L: Local - E: Enclosing - G: Global - B: Built-in Example: x = "global" def outer(): x = "enclosing" def inner(): x = "local" print(x) inner() outer() - Output: "local" -------------------------------------------------- Environment =========== - The *environment* is the collection of active namespaces. - Determines how names are resolved during execution. - Environments change dynamically during function calls. -------------------------------------------------- Scope ===== - Scope defines where a name is accessible. Types: ------ - Local Scope: inside functions - Enclosing Scope: outer functions - Global Scope: module level - Built-in Scope: Python's predefined names -------------------------------------------------- Global and Nonlocal Keywords ============================ global: ------- - Declares a name as global inside a function. Example: x = 10 def change(): global x x = 20 nonlocal: --------- - Refers to a variable in the enclosing scope. Example: def outer(): x = 10 def inner(): nonlocal x x = 20 inner() return x -------------------------------------------------- Lifetime of Names ================= - Names exist as long as their namespace exists. Examples: - Local variables: destroyed after function execution - Global variables: exist until program ends -------------------------------------------------- Aliasing and Mutability ======================= Aliasing: --------- - Multiple names refer to the same object. Example: a = [1, 2] b = a Mutability: ----------- - Mutable objects can be changed (list, dict) - Immutable objects cannot (int, str, tuple) Effect: a.append(3) # b also sees the change -------------------------------------------------- First-Class Objects ===================== - Everything in Python is an object. - Names can refer to functions, classes, etc. Example: def greet(): print("Hello") f = greet f() -------------------------------------------------- Garbage Collection ================== - Python automatically manages memory. - Objects are deleted when no names reference them. -------------------------------------------------- Summary ======= - Names are references to objects. - Namespaces store name-object mappings. - LEGB rule determines name resolution. - Scope defines visibility. - Environment is the active set of namespaces. - Python uses reference semantics and automatic memory management. -------------------------------------------------- Quick Reference =============== +------------------+----------------------------------+ | Concept | Description | +==================+==================================+ | Name | Identifier for an object | +------------------+----------------------------------+ | Namespace | Mapping of names to objects | +------------------+----------------------------------+ | Scope | Region where name is accessible | +------------------+----------------------------------+ | Environment | Active namespaces at runtime | +------------------+----------------------------------+ | LEGB Rule | Name resolution order | +------------------+----------------------------------+