A Python program accesses data values through references. A reference is a name that refers to the location in memory of a value (object). References take the form of variables, attributes, and items. In Python, a variable or other reference has no intrinsic type. The object to which a reference is bound at a given time always has a type, but a given reference may be bound to objects of various types during the program's execution.
In Python there are no declarations. The existence of a variable begins with a statement that binds the variable, or, in other words, sets a name to hold a reference to some object. You can also unbind a variable, resetting the name so it no longer holds a reference. Assignment statements are the most common way to bind variables and other references. The del statement unbinds references.
Binding a reference that was already bound is also known as rebinding it. Whenever I mention binding in this book, I implicitly include rebinding except where I explicitly exclude it. Rebinding or unbinding a reference has no effect on the object to which the reference was bound, except that an object disappears when nothing refers to it. The automatic cleanup of objects bereft of references is known as garbage collection.
You can name a variable with any identifier except the 30 that are reserved as Pythons keywords (see "Keywords" on page 35). A variable can be global or local. A global variable is an attribute of a module object (Chapter 7 covers modules). A local variable lives in a functions local namespace (see "Namespaces" on page 76).
Object attributes and items
The main distinction between the attributes and items of an object is in the syntax you use to access them. An attribute of an object is denoted by a reference to the object, followed by a period (.), followed by an identifier known as the attribute name (for example, x. y refers to one of the attributes of the object bound to name x , specifically that attribute which is named y ).
An item of an object is denoted by a reference to the object, followed by an expression within brackets (). The expression in brackets is known as the item's index or key, and the object is known as the item's container (for example, x[y] refers to the item at the key or index bound to name y , within the container object bound to name x ).
Attributes that are callable are also known as methods. Python draws no strong distinctions between callable and noncallable attributes, as some other languages do. All rules about attributes also apply to callable attributes (methods).
Accessing nonexistent references
A common programming error is trying to access a reference that does not exist. For example, a variable may be unbound, or an attribute name or item index may not be valid for the object to which you apply it. The Python compiler, when it analyzes and compiles source code, diagnoses only syntax errors. Compilation does not diagnose semantic errors, such as trying to access an unbound attribute, item, or variable. Python diagnoses semantic errors only when the errant code executes, i.e., at runtime. When an operation is a Python semantic error, attempting it raises an exception (see Chapter 6). Accessing a nonexistent variable, attribute, or item, just like any other semantic error, raises an exception.