Data + Processing = Objects
Combining Data and Processing into Class Definitions
In Part I, “Language Basics”, we examined the core statements in the
Python language. In Part II, “Data Structures”, we examined the built-in data
structures available to us as programmers. Using these data structures
gave us some hands-on experience with a number of classes. After using
this variety of built-in objects, we are better prepared to design our own
objects.
Chapter 21, Classes
introduces basics of class
definitions and Chapter 22, Advanced Class Definition
introduces simple
inheritance. We extend this discussion further to include several common
design patterns that use polymorphism. In Chapter 23, Some Design Patterns
we cover a few common design patterns. Chapter 24, Creating or Extending Data Types
describes the mechanism for adding types to
Python that behave like the built-in types.
We will spend some additional time on Python's flexible notion of
"attribute" in Chapter 25, Properties and Descriptors
. It turns out that an
attribute can be a simple instance variable or it can be a method function
that manages an instance variable.
We'll look at Python's decorators in Chapter 26, Decorators
; this is handy syntax for assuring that
specific aspects of a family of classes are implemented consistently.
We'll look at the various ways to define properties in objects.properties.
Additionally, we'll look how we can manage more sophisticated object
protocols in Chapter 27, Managing Contexts: the
with
Statement
.
Data Types. We've looked at most of these data types in Part II, “Data Structures”. This is a kind of road-map of some of the most
important built-in features.
-
None. A unique constant, handy as a placeholder when no other value
is appropriate. A number of built-in functions return values of
None
to indicate that no useful work can be
done.
-
NotImplemented. A unique constant, returned by special methods to indicate
that the method is not implemented. See the section called “Special Method Names” for more information.
-
Numeric types have relatively simple values.
-
Boolean. A variety of values are treated as logically false:
False
, 0, None
,
""
, ()
,
[]
, {}
. All other values
are logically True
.
-
Integer. Typically 32-bit numbers with a range of -2,147,483,648
through 2,147,483,647. On some platforms, these may be 64-bit
numbers.
-
Long. These are specially coded integers of arbitrary length.
They grow as needed to accurately represent numeric
results.
-
Float. These are floating point, scientific notation numbers.
They are represented using the platform's floating point
notation, so ranges and precisions vary. Typically these are
called "double precision" in other languages, and are often
64-bits long.
-
Complex. These are a pair of floating point numbers of the form
(
a
+
b
j),
where
a
is the real part and
b
is the “imaginary”
part.
-
Sequence. Collections of objects identified by their order or
position.
-
Immutable sequences are created as needed and can be used
but never changed.
-
String. A string is a sequence of individual ASCII
characters.
-
Unicode. A Unicode string is a sequence of individual Unicode
characters.
-
Tuple. A tuple is a simple comma-separated sequence of Python
items in ()
's.
-
Mutable sequences can be created, appended-to, changed,
and have elements deleted
-
Set and Frozenset. Collections of objects. The collection is neither ordered nor
keyed. Each item stands for itself. A set
is
mutable; we can append-to, change and delete elements from a set. A
frozenset
is immutable.
-
Mapping. Collections of objects identified by keys instead of
order.
-
Dictionary. A dictionary is a collection of objects which are indexed
by other objects. It is like a sequence of
key:value
pairs, where keys can be found
efficiently. Any Python object can be used as the value. Keys
have a small restriction: mutable lists and other mappings
cannot be used as keys. It is created with a comma-separated
list of key:value
pairs in
{}
's.
-
Callable. When we create a function with the
def
statement, we create a callable
object. We
can also define our own classes with a special method of
__call__
, to make a callable object that
behaves like a function.
-
File. Python supports several operations on files, most notably
reading, writing and closing. Python also provides numerous modules
for interacting with the operating system's management of
files.
There are numerous additional data structures that are part of
Python's implementation internals; they are beyond the scope of this
book.
One of the most powerful and useful features of Python, is its
ability to define new classes. The next chapters
will introduce the class and the basics of object-oriented
programming.