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 Python’s keywords (see Keywords). 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 function’s local namespace (see Namespaces).
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).
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.
Assignment statements can be plain or augmented. Plain assignment to a variable (e.g., name
=
value
) is how you create a new variable or rebind an existing variable to a new value. Plain assignment to an object attribute (e.g., x
.
attr
=
value
) is a request to object x
to create or rebind attribute attr
. Plain assignment to an item in a container (e.g., x
[
k
]=
value
) is a request to container x
to create or rebind the item with index k
.
Augmented assignment (e.g., name
+=
value
) cannot, per se, create new references. Augmented assignment can rebind a variable, ask an object to rebind one of its existing attributes or items, or request the target object to modify itself (an object may, of course, create whatever it wants in response to such requests). When you make a request to an object, it is up to the object to decide whether to honor the request or raise an exception.
A plain assignment statement in the simplest form has the syntax:
target = expression
The target is also known as the lefthand side (LHS), and the expression is the righthand side (RHS). When the assignment executes, Python evaluates the RHS expression, then binds the expression’s value to the LHS target. The binding does not depend on the type of the value. In particular, Python draws no strong distinction between callable and noncallable objects, as some other languages do, so you can bind functions, methods, types, and other callables to variables, just as you can numbers, strings, lists, and so on.
Details of the binding do depend on the kind of target, however. The target in an assignment may be an identifier, an attribute reference, an indexing, or a slicing:
- An identifier
Is a variable’s name. Assignment to an identifier binds the variable with this name.
- An attribute reference
Has the syntax
obj
.
name
.obj
is an arbitrary expression, andname
is an identifier, known as an attribute name of the object. Assignment to an attribute reference asks objectobj
to bind its attribute namedname
.- An indexing
Has the syntax
obj
[
expr
]
.obj
andexpr
are arbitrary expressions. Assignment to an indexing asks containerobj
to bind its item indicated by the value ofexpr
, also known as the index or key of the item in the container.- A slicing
Has the syntax
obj
[
start
:
stop
]
orobj
[
start
:
stop
:
stride
]
.obj
,start
,stop
, andstride
are arbitrary expressions.start
,stop
, andstride
are all optional (i.e.,obj
[:
stop
:]
andobj
[:
stop
]
are also syntactically correct slicings, equivalent toobj
[None:
stop
:None]
). Assignment to a slicing asks containerobj
to bind or unbind some of its items. Assigning to a slicing such asobj
[
start
:
stop
:
stride
]
is equivalent to assigning to the indexingobj
[slice(
start
,stop
,stride
)]
, whereslice
is a Python built-in type (seeslice
in Built-in Types) whose instances represent slices.
I’ll come back to indexing and slicing targets when I discuss operations on lists, in Modifying a list, and on dictionaries, in Indexing a Dictionary.
When the target of the assignment is an identifier, the assignment statement specifies the binding of a variable. This is never disallowed: when you request it, it takes place. In all other cases, the assignment statement specifies a request to an object to bind one or more of its attributes or items. An object may refuse to create or rebind some (or all) attributes or items, raising an exception if you attempt a disallowed creation or rebinding (see also _ _setattr_ _
in General-Purpose Special Methods and _ _setitem_ _
in Container methods).
You can give multiple targets and equals signs (=
) in a plain assignment. For example:
a = b = c = 0
binds variables a
, b
, and c
to the same value, 0
. Each time the statement executes, the RHS expression is evaluated just once, no matter how many targets are part of the statement. Each target then gets bound to the single object returned by the expression, just as if several simple assignments executed one after the other.
The target in a plain assignment can list two or more references separated by commas, optionally enclosed in parentheses or brackets. For example:
a, b, c = x
This statement requires x
to be an iterable with exactly three items, and binds a
to the first item, b
to the second, and c
to the third. This kind of assignment is known as an unpacking assignment. The RHS expression must be an iterable with exactly as many items as there are references in the target; otherwise, Python raises an exception. Each reference in the target gets bound to the corresponding item in the RHS. An unpacking assignment can also be used to swap references:
a, b = b, a
This assignment statement rebinds name a
to what name b
was bound to, and vice versa.
An augmented assignment differs from a plain assignment in that, instead of an equals sign (=
) between the target and the expression, it uses an augmented operator, which is a binary operator followed by =
. The augmented operators are +=
, -=
, *=
, /=
, //=
, %=
, **=
, |=
, >>=
, <<=
, &=
, and ^=
. An augmented assignment can have only one target on the LHS; augmented assignment doesn’t support multiple targets.
In an augmented assignment, just as in a plain one, Python first evaluates the RHS expression. Then, if the LHS refers to an object that has a special method for the appropriate in-place version of the operator, Python calls the method with the RHS value as its argument. It is up to the method to modify the LHS object appropriately and return the modified object (Special Methods covers special methods). If the LHS object has no appropriate in-place special method, Python applies the corresponding binary operator to the LHS and RHS objects, then rebinds the target reference to the operator’s result. For example, x
+=
y
is like x
=
x
._ _iadd_ _(
y
)
when x
has special method _ _iadd_ _
. Otherwise, x
+=
y
is like x
=
x
+
y
.
Augmented assignment never creates its target reference; the target must already be bound when augmented assignment executes. Augmented assignment can rebind the target reference to a new object or modify the same object to which the target reference was already bound. Plain assignment, in contrast, can create or rebind the LHS target reference, but it never modifies the object, if any, to which the target reference was previously bound. The distinction between objects and references to objects is crucial here. For example, x
=
x
+
y
does not modify the object to which name x
was originally bound. Rather, it rebinds the name x
to refer to a new object. x
+=
y
, in contrast, modifies the object to which the name x
is bound when that object has special method _ _iadd_ _
; otherwise, x
+=
y
rebinds the name x
to a new object, just like x
=
x
+
y
.
Despite its name, a del
statement does not delete objects; rather, it unbinds references. Object deletion may automatically follow as a consequence, by garbage collection, when no more references to an object exist.
A del
statement consists of the keyword del
, followed by one or more target references separated by commas (,
). Each target can be a variable, attribute reference, indexing, or slicing, just like for assignment statements, and must be bound at the time del
executes. When a del
target is an identifier, the del
statement means to unbind the variable. If the identifier was bound, unbinding it is never disallowed; when requested, it takes place.
In all other cases, the del
statement specifies a request to an object to unbind one or more of its attributes or items. An object may refuse to unbind some (or all) attributes or items, raising an exception if you attempt a disallowed unbinding (see also _ _delattr_ _
in General-Purpose Special Methods and _ _delitem_ _
in Container methods). Unbinding a slicing normally has the same effect as assigning an empty sequence to that slice, but it is up to the container object to implement this equivalence.
Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.