This blog focusses on the Python Objects And Classes. Please go through the previous blog related to OOPs before starting this blog https://ainewgeneration.com/object-oriented-programming/. Lets dive deep into the concepts of class and object implementation in python. As you already know, before creating the object you must create a class i.e. class is design for an object. Let’s jump into more details.
Table of Contents
- Python Objects And Classes
- Define a Class in Python.
- Creating Object in Python.
- self in oops.
- Constructors in Python.
- Destroying object in python.
Python Objects And Classes
Python is a multiple paradigm language. It means Python supports different programming paradigms like Functional Programming, Object Oriented Programming, and Procedure Oriented Programming.
- Procedure Oriented Programming : Whenever we work with Python, we define function over function. Suppose you want to develop a software application, you will break down the project to small parts and those small modules will be a function.
- Functional Oriented Programming : When you are using function inside the function to create an application, they are immutable. Once if the function is defined it can’t be changed . Example: Lambda Function.
- Object Oriented Programming : OOPs in Python is all about creating an object. The main aim of OOPs is to bind together the data and function in one place.
Class is a blueprint where users define their data types which hold their own data members and member function which can be accessed and used by creating objects or instances of the class.
The object will have something where you can store the data and their behaviors. It is used to store something in the object. Every object will have certain attributes and behaviors. For example, a person’s name, age, height, profession are the attributes and the Person’s actions such as talking, dancing, singing, walking are defined as the behaviors.
Let us explain this using a real world example. Lets say you are using Iphone 12. There shall be many people using the same mobile. Every individual user is an object here. But if you see the characteristics of the phone, the model is designed only once and it shall be the same for all the Iphone 12 mobiles that is manufactured and this can be taken as class in OOPs. We can say that the class is designed and the object is an instance of class.
Define a Class in python
We define function with def as keyword ex: def fun_name. In the same way, we define class by using the keyword class ex: class cls_name.
class Computer: pass
As in the above code, we have created a class computer but what will the class store? It contains 2 things Attributes & Behavior.
- Attributes : OOPs define Attributes the same way we define variables in the python.
- Behavior : The functions in OOPs are called as methods. It is where you can define the behavior.
Now defining methods in class computer.
class Computer: comp_name = "Lenovo" def configuration(self): print("computer config:","i7-16GB-1TB")
When we define class, a new class object is created with the same class name. A class object allows us to access attributes as well as instantiate new objects of that class.
print(Computer.comp_name) print(Computer.configuration) output: "Lenovo" <function __main__.computer.configuration>
Creating an Object in Python
As seen above, with the class objects we can access different attributes as well as we can create a new object instance of the class. The way to create an object is similar to that of the function call.
comp_1 = Computer()
In the above code, we have created a new object instance of a class computer named comp_1. Now we can access different attributes and methods of class using object name prefix. You may already know that we can create ‘n’ number of the objects for computer class i.e., comp_1,comp_2 …. comp_n.
#creating object of computer class comp_1 = Computer() #Calling configuration method comp_1.configuration() output : computer config : i7 -16GB -1TB
self in OOPs
You may have noticed the self parameter when we have to define configuration method in the class computer, we pass self as a parameter and when we create object of computer comp_1 to access method of computer class it works when we called comp_1.configuration() without passing any parameter.
The reason behind for not passing any parameter in place of self as whenever an object call its method the object itself passes as a first argument so comp_1.configuration() translates into comp_1.configuration(comp_1).
Note: The first argument of a function in class must be the object itself which is called as self. So calling any method with n number of arguments is equivalent to calling a function with the first argument as the object itself follows the rest n number of the argument.
Constructors in Python
The methods in the class that begin with double underscore __ are called special functions and they have special meanings.
One of the special methods is __init__ which acts as a constructor in OOPs Python. Whenever a new object of a particular class is instantiated, the __init__ method is called automatically.
We use __init__ method to specifically initialize a variable.
class Computer: # constructor def __init__(self ,cpu ,ram): self.cpu = cpu self.ram = ram # method def configuration(self): print("Config is:",self.cpu ,self.ram) #create an objects of computer class comp_1 = Computer("i7","16GB") comp_2 = Computer("i5","8GB") comp_3 = Computer("i3","8GB") #called configuration method comp_1.configuration() comp_2.configuration() comp_3.configuration() output: Config is: i7 16GB Config is: i5 8GB Config is: i3 8GB
As in the above code, we create a class name as Computer with the first method. In the class, __init__ automatically gets called up when an object is created of the class.
In __init__ method we passed 3 parameters. The first one is self and the rest two is cpu and ram. When you create an object of a particular class computer; __init__ method automatically gets called up by taking only 2 arguments i.e. cpu & ram. In place of self parameter, object name will be passed by default i.e. comp_1 = computer(comp_1, “i7”, “16GB”).
Destroying objects in python.
Deleting objects in python can be done by using the del keyword. In the above, we created different objects of computer class comp_1, comp_2 & comp_3. To destroy an object we can use the keyword del.
# Destroying object del comp_1 #checking object comp_1 comp_1.configuration() output: NameError: name 'comp_1' is not defined
In the above code, we have deleted the comp_1 object from the namespace but still, the comp_1 object is present in memory space later. If no name comp_1 is further assigned then Python automatically destroys the memory location for comp_1. This process is called garbage collection in python.
Namespace : It is an area where you can create and store objects/variables.
I hope you are clear with the concepts of classes and objects in Python. In the next blog, we will focus on types of Variables and Methods in Python