liveBook · Manning

Binding

As explained in Python How-To

Name binding is the association of an identifier with the program entity or value it denotes.

Binding associates an identifier with the program entity or value it denotes. In Python, assigning a value makes a variable available; initialize it when later code may need it, since an unassigned variable cannot be resolved. Reassigning the same variable can give it a value of a different type. section 5.4.5 section 6.3.1

Keep these distinctions in mind:

  • Type hints annotate variables but do not enforce their types; a different type can still be assigned. section 6.3.1
  • A function’s local variables can be inspected in the debugger with `p` or `locals()`. section 13.2.4
(Pdb) locals()
{'inject_bug': True, 'task_text': 'Laundry,3#', 'title': 
 'Laundry', 'urgency_text': '3#'}

Inspecting the local namespace shows the variables and values available inside the function. section 13.2.4

How Python How-To covers it

How deep it goes
Explains initialization, reassignment, type hints, and local-value inspection, but not a general name-resolution model.
Examples
Uses reassignment from an integer to a string, task-variable initialization, and debugger inspection of local values.
What it assumes
Assumes familiarity with common Python data models such as strings, lists, tuples, and dictionaries.
What you can do after
Diagnose function state by inspecting individual variables or the local namespace in the debugger.
How it connects
Connects variables to dynamic typing, type hints, scope, and local namespaces.

Also explained in

Compare how 11 books explain binding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.