Variables and Other References
A Python program accesses data values through references. A reference is a name that refers to the specific 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 does have a type, however. Any given reference may be bound to objects of different types during the execution of a program.
Variables
In
Python, there are no declarations. The existence of a variable
depends on a statement that binds the variable,
or, in other words, that sets a name to hold a reference to some
object. You can also unbind a variable by
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 binding is mentioned in
this book, rebinding is implicitly included except where it is
explicitly excluded. 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 to which there are no references is known as
garbage
collection.
You can name a variable with any identifier except the 29 that are reserved as Python’s keywords (see Section 4.1.2.2 earlier in this chapter). A variable ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access