Introduction to Object-Oriented Programming(OOPS)
Now, we will see object-oriented programming in Python. First will look into the basics revolve around the oops.
So, when someone is talking about oops then directly one thing comes into the mind there will be classes and objects. Some methods and variables associated with those objects. Let’s dig into the detail of what is class, object actually?
Let’s take the example of a company. In a company, we have employees of different profiles like Data Scientist, Data Analyst, Software Developer, Human Resource managers, etc.
So, in that case, the company name is our class and objects will be different profiles employee and methods and function will be like a task() – which describe their work, timings() – what is the office hours for each profile people. This is an example which may give you an overview of classes and objects and methods associated with those classes.
Now, we will talk in terms of Python like how to create a class in Python and its constructor function, etc.
In Python, classes are created using “class” keyword and variables are the attributes. In class, we have a self keyword which actually works as an object and it helps in accessing the class variables and some other things will be clear to you through example.
1. Creating a simple class
Let’s see how to create a class and class variable. How to access a class variable?
2. Instance variable and how its value change for different objects.
In this section, we will check how to create an instance variable and using self keyword how to change its value whenever we are creating a new object.
3. __init__() function – Constructor and self keyword
In this section, we will see the __init__() function which is basically a constructor function. A constructor function is called every time whenever we are creating an object of that class.
I hope this coding section gives you a clear idea about the class variable, instance variable, __init__ function, self keyword and how to use the class variable inside the defined function, etc.
Hope you are enjoying learning intermediate Python with us. Stay tuned!
Keep learning with us.