In this article, we’ll learn what Python class is and how to properly define and use it in code.
We’ll be using multiple practical Python code examples with step by step explanation to better understand how Python class works.
After reading this article, you’ll have a practical knowledge of how to use Python classes.
So without wasting anymore time, let’s just throw ourselves into its practical implementation.
What is Python Class?
As you may already know that Python is an object-oriented programming language. It possess the concept of objects and classes.
An object can be defined as a collection of data like variables and methods (functions) that act on that data. Similarly, a class works as a blueprint for object.
There can be many objects of the same class. Class has attributes that the object can use to perform actions. These attributes can be a function, variable etc.
Let’s now understand the concept of Python class using practical examples. But let’s first understand its syntax.
Syntax of Python Class
class ClassName: // body of class
It starts with the class keyword.
Then comes name of the class and this line ends with a colon(:).
Use indentation to specify the body of class. You can specify variables or functions in it.
Implementing Python Class (Multiple Examples)
Below examples will give you a proper idea of how to define and use Python class.
Example 1: Define a Class
class ClassName: """This is a doc string"""
A class is created. We’ve also used a doc string in it. Doc string can be used as a first statement in the class. It specifies all the details about that class.
Use triple quotes to write this doc string in multiple line. Although, doc string not mandatory but specifying it is a good practice.
Example 2: Create Variable in Class
class ClassName: value='I am inside class'
This is how you can create a variable inside a class. Let’s now see how to fetch its value.
When a new class is created, then an object with the same name is also created. It can be used to fetch data from that class and also to create new objects of the same class.
Let’s now fetch the data of variable using this object.
class ClassName: value='I am inside class' print(ClassName.value)
I am inside class
Example 3: Create an Object of Class
This is how you can create an object of some specific class. You can specify any name to that object.
Let’s now see how to fetch the above created variable data using a newly created object. See below code:
class className: value='I am inside class' obj=className // new object created print(obj.value) // it will print the data of value variable of class
I am inside class
You can see that we’ve fetched the data of variable using( objectName.classVariableName ).
Example 4: Create Function in Class
class ClassName: def classFun(): // function created print('I am inside function of a class') objFun=ClassName objFun.classFun() // function called using object of the same class
I am inside function of a class
We’ve created a function in class and then we’ve called that function using the object of that same class.
Example 5: Passing Self as a Function Parameter
class ClassName: def classFun(self): // self will store the object print('My name is '+self.name) objFun=ClassName() objFun.name='Yasir' // create a variable attribute of that class and initialize it objFun.classFun() // My name is Yasir
My name is Yasir
You can see that we’ve passed a self parameter to the function of class. It stores the value of object which is calling it.
We also have created a new variable using the newly created object of that same class and initializes it.
By using the self parameter, we’ve fetched the value of variable inside a function.
Example 6: Constructors in Python Class
They look like a normal function in class, functions that starts with double underscore(__) are called special functions as they have a special meaning.
Let’s look at a special function that is called automatically whenever a new object of that class is instantiated. See below code:
class ClassName: var=0 def __init__(self,val=5): // constructor var=val print(var) objFun=ClassName()
You can see that we’ve specified a variable of class and have given it a value 0. Then we’ve created a constructor that’ll be called automatically whenever a new object of that class is created.
To demonstrate, we’ve created a new object and as a result, this constructor is called automatically and it prints the new value assigned to the class variable. You can easily use it to initialize the variables automatically.
So this is how you can create and fetch data from Python class. In future articles, we’ll explain more features of Python classes.
Feel free to ask if you still have any questions regarding the implementation of Python class. I’ll be very happy to answer all your questions.
To conclude, hope you now have a detailed practical understanding of how to define and use Python class. I’d be delighted to have your feedback on this post. Thank you for reading it.