Understanding the Python Class as a Packaging Method

By John Paul Mueller

A class is essentially a method for packaging Python 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. 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 instance 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 method of a class, so they pose the potential problem of class contamination.

    Global variables have always been considered a bad idea in programming, doubly so in Python, because every instance can see the same information. In addition, data hiding really doesn’t work in Python. Every variable is always visible. The most important thing to remember in Python is that it lacks the data hiding found in other languages to promote true object orientation.

  • 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 or perform a task with different variable types.
  • 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 by 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.