Classes, objects and methods in Python
Coding with Python
🕑 This lesson will take about 20 minutes
In this lesson, you will learn how to create classes and objects in Python code using the object-oriented paradigm. Before we look at how to create our own classes and objects, let’s first define what object-oriented programming is.
Object-oriented programming
Object-oriented programming (OOP) is a type of programming where programmers can define not just the data type of a data structure, but also the different types of operations (or functions) that can be applied to that data structure. Under this model, a data structure is an object which consists of both data and functions.
Imperative and procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming (OOP) is about creating objects that contain both data and functions.
Programmers can create relationships between different objects. For example, objects can be given characteristics that belong to another object.
Using an object-oriented approach involves considering each component of a problem as a self-contained unit. Each unit is able to remember things (data/attributes), and do things (methods/operations).
Classes
A class is a blueprint or template that describes what something is, such as the idea of a button in an app, or an enemy in a game, or a product on an online store.
Classes will exist for different parts of the program. Many languages have predefined classes.
All classes define attributes (sometimes known as properties) and behaviours (sometimes known as methods) that objects created from that class will have.
A class will often provide initial (or default) values for attributes.
Objects
An object is an ‘instance’ of a class and can perform any of the methods defined by the class’ code. It is an element created with the properties and methods defined by the class.
Objects are instantiated from a class using a method called a constructor.
Instantiation is the creation of an instance of an object from a class. When a new object is created from a class, it is “instantiated”. To instantiate a class, means to create an object from the class.
A constructor is a special type of method/function defined in a class that is called to create an object from that class. It prepares the new object for use, often accepting arguments that the constructor uses to set required class attributes/variables.
Example scenario
Let’s say we’re designing the GUI (graphical user interface) for an app that will have many buttons. We want to create a class that will act as a blueprint, so that we can reuse a lot of existing code to make many buttons that have different values for their properties (eg. each button might be a different colour or size, or perform a different action when clicked). We can create a class called ‘Button’ that defines the attributes a button can have and the behaviours/methods it can perform.
Class attributes for a button may include:
text
backgroundColour
width
height
font
fontSize
onClick
etc.
Class methods for a button may include:
Create (creates a new button)
Hide (hides a button)
Show (shows a button)
SetBackgroundColour (changes the background colour of a button)
etc.
Now, when creating a new button in your app, you can re-use the existing code that creates a button and just specify the values for its properties (eg. "green" for the backgroundColour property, or 100 for the height property).
Classes, objects, and methods in Python
Here is an example of how to create a class and create objects from that class in Python code. In this example, we have a class called Player that has properties/attributes like name, team, health, and score. It also has behaviours/methods such as increaseScore (which increases the player’s score by a specified amount when called), decreaseHealth (where the player takes a specified amount of damage when called to reduce the player’s health), and displayInfo (which displays the player’s details and stats when called).
In this example, there is also a constructor method (called __init__ ). When a new Player object is created from the class, only the player’s name and team need to be specified, for example, my_player = Player("Joe", "Tigers") , and the player is automatically given an initial value of 3 for health and 0 for score.
At the bottom of the code, there is an example of a new object called my_player being instantiated from the Player class as well as the different behaviours/methods being called on the object.
In the next lesson, we will look at inheritance - another important concept in object-oriented programming.
Next lesson: Inheritance