Using Classes in Python
You’ve already worked with a number of classes in previous Posts. Many of the examples are easy to construct and use because they depend on the Python classes. Even though classes are briefly mentioned in previous Posts, those Posts largely ignore them simply because discussing them wasn’t immediately important.
Classes make working with Python code more convenient by helping
to make your applications easy to read, understand, and use. You use classes to
create containers for your code and data, so they stay together in one piece.
Outsiders see your class as a black box — data goes in and results come out.
At some point, you need to start constructing classes of your own
if you want to avoid the dangers of the spaghetti code that is found in older
applications. Spaghetti code is much as the name implies — various lines of
procedures are interwoven and spread out in such a way that it’s hard to figure
out where one piece of spaghetti begins and another ends. Trying to maintain
spaghetti code is nearly impossible, and some organizations have thrown out
applications because no one could figure them out.
Besides helping you understand classes as a packaging method that
avoids spaghetti code, this Post helps you create and use your own classes for
the first time. You gain insights into how Python classes work toward making
your applications convenient to work with. This is an introductory sort of Post,
though, and you won’t become so involved in classes that your head begins to
spin around on its own. This Post is about making class development simple and
manageable.
Understanding the Class as a Packaging Method
A class is essentially a method for packaging code. The idea is to
simplify code reuse, make applications more reliable, and reduce the potential
for security breaches. Well-designed classes are black boxes that accept
certain inputs and provide specific outputs based on those inputs. In short, a
class shouldn’t create any surprises for anyone and should have known (quantifiable)
behaviors. How the class accomplishes its work is unimportant, and hiding the
details of its inner workings is essential to good coding practice.
Before you move onto actual class theory, you need to know a few
terms that are specific to classes. The following list defines terms that you
need to know in order to use the material that follows later in the Post. These
terms are specific to Python. (Other languages may use different terms for the
same techniques or define terms that Python uses in different ways.)
✓ Class: Defines
a blueprint for creating an object. Think of a builder who wants to create a
building of some type. The builder uses a blueprint to ensure that the building
will meet the required specifications. Likewise, Python uses classes as a
blueprint for creating new objects.
✓ Class
variable: Provides a storage location used by all methods in an instance of the
class. A class variable is defined within the class proper but outside of any
of the class methods. Class variables aren’t used very often because they’re a
potential security risk — every method of the class has access to the same
information. In addition to being a security risk, class variables are also
visible as part of the class rather than a particular instance of a class, so
they pose the potential problem of class contamination.
Data member: Defines either
a class variable or an instance variable used to hold data associated with a
class and its objects.
Function overloading:
Creates more than one version of a function, which results in different
behaviors. The essential task of the function may be the same, but the inputs
are different and potentially the outputs as well. Function overloading is used
to provide flexibility so that a function can work with applications in various
ways.
Inheritance: Uses a parent
class to create child classes that have the same characteristics. The child
classes usually have extended functionality or provide more specific behaviors
than the parent class does.
✓ Instance:
Defines an object created from the specification provided by a class. Python
can create as many instances of a class to perform the work required by an
application. Each instance is unique.
Instance variable: Provides
a storage location used by a single method of an instance of a class. The
variable is defined within a method.
Instance variables are considered safer than class variables
because only one method of the class can access them. Data is passed between
methods using arguments, which allows for controlled checks of incoming data
and better control over data management.
✓ Instantiation:
Performs the act of creating an instance of a class. The resulting object is a
unique class instance.
Method: Defines the term
used for functions that are part of a class. Even though function and method
essentially define the same element, method is considered more specific because
only classes can have methods.
Object: Defines a unique
instance of a class. The object contains all the methods and properties of the
original class. However, the data for each object differs. The storage locations
are unique, even if the data is the same.
Operator overloading:
Creates more than one version of a function that is associated with an operator
such as: +, -, /, or *, which results in different behaviors. The essential
task of the operator may be the same, but the way in which the operator
interacts with the data differs. Operator overloading is used to provide
flexibility so that an operator can work with applications in various ways.
Considering the Parts of a Class
A class has a specific construction. Each part of a class performs
a particular task that gives the class useful characteristics. Of course, the
class begins with a container that is used to hold the entire class together,
so that’s the part that the first section that follows discusses. The remaining
sections describe the other parts of a class and help you understand how they
contribute to the class as a whole.
0 comments:
Post a Comment