Classes and Objects
Classes and Objects
Classes and Objects
Specification – What data we are working with and what can be done with it
Implementation – How the data is stored and how it is actually manipulated
When using someone else’s code, the specification is more important than the implementation.
i.e. it is more important to know that there is a method to search lists and how to use that method than
it is to know what search algorithm was used in that method.
Data Encapsulation – The combination of data and related functions into a single component
The use of data encapsulation allows us to adhere to a design principle known as the Separation of
Concerns. This involves dividing a project into sections which each address a separate concern. By
utilising this principle, the development and maintenance of programs can be simplified as any one
section of the code can be edited without having to know the details of other sections or needing to
make any corresponding changes in other sections of the project.
These ideas can be implemented in practice by using classes and objects of those classes.
Classes – A class is a user-defined data type. The type can include data (attributes) and functions for
use specifically with that data (methods)
Classes can be used by creating an instance of a class. This is done using a function-like call, shown
below. These calls are called ‘object instantiation’. A single class can have many objects/instances at
any given time.
x = MyClass()
#x now represents an instance of the class MyClass
Defining classes
Classes must be defined before they are called, and like all control structures they start with a header.
This is followed by a block of indented code known as the body. The body has:
- The definition of any class attributes
o Class attributes – Data stored once for use by any instance of the class
- An instantiation method __init__ to be run after every object definition
o This sets object attributes and ‘sets up’ the object for use
- The other methods included in the class
o Object methods (methods where the first argument is an object)
o Class methods (for use with class attributes)
o Any other special methods (such as __str__ or __add__)
Note that class names should begin with a capital letter whilst function names should begin with a
lowercase letter in order to avoid any possible confusion.
Special methods
Special methods are object methods which get used elsewhere by the Python Interpreter, they must
have the correct name. Their names begin and end with two underscores such as __init__.
Every class will have at least one special method in the form of its __init__ method. Any other special
methods are optional and do not have to be present in every class.
Object instantiation
When an object is defined (using the function-like call seen earlier) Python will do the following:
1. Create a namespace (assign memory) for the object
2. Run the __init__ method on this new object
This means that the instantiation calls must have the arguments required by the __init__ method.
Private attributes
Private attributes are attributes which are only accessible to methods within the class definition. Their
names begin with a double underscore in Python. They cannot be accessed directly from outside of
the class (uses name mangling to prevent this). The only way to access a private attribute is by a call
to a method within the class (if one exists).
If you want to be able to view private attributes you must write methods to do this known as
accessors/getters.
Likewise, if you want to be able to edit private attributes, you must write methods to do this known as
mutators/setters.
Python has weak support for private attributes as using them is not considered pythonic because
Python is an interpreted programming language so to run the code the user must have the source code
meaning nothing can really be private. In other languages however, private attributes can be a
fundamental part of the language.
Private attributes provide no real security as the data is still accessible, it is just stored under a
different name (name mangling). Also, the source code is relatively easy to find and alter meaning the
attributes can be made public easily by anyone with the source code and a basic understanding of
Python.
Instead of being a security measure, private attributes should be thought of as being like
documentation in that they make it clear to a user what use-cases your code is designed to support and
if they start altering private attributes when they shouldn’t any consequences are their own fault and
not the fault of the developer who created the original code.