Object-Oriented Python

Object-Oriented Programming (OOP) is based around the concept of Objects, where an Object is made up of Attributes, or Variables, that can contain data, and Methods, that describe actions of an Object. Attributes and Methods are collectively known as members of a class. A person can be used as a real-world example of an Object. A person has Attributes such as height, weight, eye colour, hair colour and so on, as well as Methods or actions such as, walk, talk and eat.

A Class is a blue print, or template, of an Object that describes its Attributes and Methods. From this template one or more Objects can be created, which is known as instantiating an Object. A Class can be compared to an architects drawing of a building, which describes how it needs to be built. From this drawing, one or more buildings can be made to the specification of the drawing.

In Python, the basic structure of a Class is as follows.

class ClassName:

    # Attributes and methods go here

Notice the capitalisation of the first letter of each word in the Class name. This is known as Pascal Case and is the standard method for naming Classes in Python. As with other areas of the Python programming language, indentation is key, Attributes and Methods must be indented below the Class definition in order for them to be a part of that particular Class.

Below is an example of a Class called ‘Greeting’, along with how it can be instantiated, using the name ‘greeting’.

class Greeting:

    # Attributes and methods go here


# Instance of the class Greeting called 'greeting'
greeting = Greeting()

Adding Attributes

Attributes are added to Classes in Python in the form of Variables.

class Greeting:

    # Attributes
    message = "Hello World!"


# Class instance
greeting = Greeting()

# Display the class attribute 'message' in the console
print(greeting.message)

Here an Attribute, ‘message’, has been added to the ‘Greeting’ Class. After the Object has been instantiated, the message is printed out to the console.

Setting Attributes

Setting an Attribute of an Object is done in a similar way to accessing it. In this example a ‘name’ Attribute has been added to the ‘Greeting’ class. This is set to ‘Fred’ and concatenated with the rest of the message before being output to the console.

class Greeting:

    # Attributes
    message = "Hello"
    name = ""


# Class instance
greeting = Greeting()

# Set attribute
greeting.name = "Fred"

# Format the greeting and display it in the console.
print(greeting.message + " " + greeting.name + "!")

Adding Methods

Functions in a Class in Python are known as Methods and carry out the actions of an Object. In the previous example the message is formed by concatenating two Attributes together, separated by a space, with an exclamation mark on the end. This formatting of the message could be carried out in a Method. The advantage of doing this is that it could be reused any number of times and if the format of the message needs changing it only needs to be altered in one place. In the ‘return’ statement, ‘self’ is used to reference the current object and is also included in the method definition, as an argument. The difference between referencing an Attribute and a Method is that with a Method the name is followed by ‘()’, as with ‘display_greeting()’ below.

class Greeting:

    # Attributes
    message = "Hello"
    name = ""

    # Method
    def display_greeting(self):

        # Return the formatted greeting
        return self.message + " " + self.name + "!"


# Class instance
greeting = Greeting()

# Set attribute
greeting.name = "Fred"

# Display greeting
print(greeting.display_greeting())

Getters and Setters

It is good practice to provide Getter and Setter Methods for Class Attributes. This allows for things such as validation before an Attribute is set, or checking if a user is authorised to access an Attribute, for example. Even if there is no initial need for any kind of processing before getting or setting an Attribute, for future proofing purposes it is still a good idea to create Getters and Setters for each Attribute where needed. With this in mind the above example can be re-written as follows.

Both Getter and Setter Methods have been added for the ‘message’ and ‘name’ Attributes. In other programming languages, such as Java and C#, it is possible to stop direct access to an Attribute and only have them accessible through Getter and Setter Methods, using what are called Access Modifiers, however, this isn’t possible to do in Python. Even with a Getter and Setter Method for an Attribute, it is still possible to access it directly.

class Greeting:

    # Attributes
    message = "Hello"
    name = ""

    # Methods
    def get_message(self):

        # Return the message attribute
        return self.message

    def set_message(self, message):

        # Set the message attribute
        self.message = message

    def get_name(self):

        # Return the name attribute
        return self.name

    def set_name(self, name):

        # Set the name attribute
        self.name = name

    def display_greeting(self):

        # Return the formatted greeting
        return self.get_message() + " " + self.get_name() + "!"


# Class instance
greeting = Greeting()

# Set the name attribute
greeting.set_name("Fred")

# Display greeting
print(greeting.display_greeting())