- How to use classes in Python?
- How to use classes in Python – defining a new class
- How to use classes in Python – the __init__() function
- How to use classes in Python – Arguments and Attributes
- How to use classes in Python – Work with class instances
- Summary
- Programming tutorials
- Learn to code and change your career!
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.
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.
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.
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to end a program in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
