How to use classes in Python – with example

  • Reading time:17 mins read

How to use classes in Python?

Python is an object-oriented programming language. Therefore, unlike procedure-oriented languages,  python makes heavy use of classes and objects. 

A class in Python is often referred to as the blueprint, template or prototype for creating objects. Classes provide us with the means of working with data and creating functions that work on the data.

Every class that we create in Python creates a new type of object which we can also refer to as the instance of that class. The process of creating this object is referred to as instantiation.

 

How to use classes in Python – defining a new class

To define a new class in Python we must use the keyword class followed by the name of the class.

The name of the class should always start with a capital letter and neither should it be separated by an underscore. Built-in classes however are typically named in lowercase.  

 

class NameOfNewClass:
    #methods
    #attributes

How to use classes in Python – the __init__() function

In this article, we are going to create a class named Product. Now that we have declared the class we are going to use the keyword def to define the special function __init__ ().

The __init__() function also known as the initializer is an equivalent of a constructor in Java and is called every time a new object is called from the class. The __init__ function allows us to initialize attributes of an object.

 

How to use classes in Python

 

Now the __init__ function accepts arguments however, we must begin with the self argument that references the current instance of the class.

How to use classes in Python – Arguments and Attributes

We can then pass in the name of the arguments that we wish to bind to their respective attributes. In this example, we are going to create the following arguments product_id, color, price and stock.

We have also created a new object ball, however,  we have not given it any arguments. Therefore, when we run this code we get an error letting us know that the positional arguments are missing.  

 

 

So we have managed to create a really basic class and also initialized some attributes. We have also created a new object.

How to use classes in Python – an example for a class instance

The new class instance is named ball, however, we have run into an error since the new object expected some positional arguments that we have not passed. In the following code sample, we have added some positional arguments.  

 

 

 

By passing some positional arguments we have managed to alleviate the error, however, there is no output as of yet.

How to use classes in Python – Work with class instances

We can go ahead and print the new object, but again this will only print the memory address of the new object.

 

   

 

Now using the object name we can access attributes of the object using the name of the object and the dot notation. In the code below, the price attribute uses the object name as shown here.  

 

 

A use case example where creating classes and objects might be very useful is a scenario such as this one.

Suppose we have lots of different prices for different types of products and we want to update a price for a specific product and not affect prices for all the other products.

In such a scenario using classes and objects can be really handy. 

 

How to use classes in Python

 

Let’s say we want to update the stock, instead of changing individual values in every instance, we can create an instance method that takes two arguments.

The self keyword to reference the current instance of the class and an argument representing the new_stock value passed when the function will be called. Now here we can see that the value of the stock is 100.

 

   

 

Now to update the stock value with the new_stock value, we are going to use dot notation and the name of the object to call the change_stock method as shown below.

The change_stock method accepts an argument which in this case is the new_stock value.  

 

 

We can also create another object with different argument values for: product_id, color, price and stock. This means that we can have multiple instances of the same class.

 

How to use classes in Python

 

Now using the change_stock method in the same way we can update values in this object that we have named bat. We are going to update the stock value as shown below.

 

 

 

Summary

This is how to use classes in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.