Python Classes And Objects

Introduction

 
In this article, I will explain Python classes and objects.
 

Definition

 
Python allows object-oriented programming languages. A Class is like a” blueprint" (Class Example: human). An object is like an instance (object Example: man, woman, children). In Python the object is another number is the method.
 

Create a class

 
Class is declared one time. In Python class keyword is used to create a class.
 
Syntax
  1. class New_Class:  
  2.       # any code  
Example
  1. class add:#class name  
  2.   a=10  
  3.   b=20  
  4.   c=a+b  
  5.   print(c)  
  6. print(add)#call the class  
Output
 
Create a class named add.
 
Python Classes And Objects
 

Create object

 
Objects are created many times. We create the class named “new_class” to create objects named “new_object”. In Object last line we must use this symbol ().
 
Syntax
  1. class New_class:  
  2.        #any code  
  3. object=New_class()  
Example
  1. class new_class:#class  
  2.   a="c# corner"  
  3.   b=20  
  4.   c=2.4  
  5. new_object=new_class()#object  
  6. print(new_object.a)  
  7. print(new_object.b)  
  8. print(new_object.c)  
Output
 
Create a class and object.
 
Python Classes And Objects
 

Object method

 
Create a method in the “new_class” class. In Python the object is another number is the method. Objects can also contain methods.
 
Example
  1. class maths:#class name  
  2.   def add(self):#function name  
  3.     a=10  
  4.     b=20  
  5.     c=a*b  
  6.     print(c)  
  7. obj =maths()  
  8. obj.add()  
Output
 
Create an object method.
 
Python Classes And Objects
 

The __init__ () function

 
The __init__ () function is used automatically every time in the class. That means for training we have to understand the built in __init__ () feature. All lessons have a function called __init__ (), which is usually finished while the elegance is being initiated. Use the__init__ () function to assign values to item properties.
 
Example
  1. class computer:#class name  
  2.   
  3.   def __init__(self,computer_name,process,ram,storage):# __init__ ()function  
  4.     self.computer_name =computer_name  
  5.     self.process =process  
  6.     self.ram = ram   
  7.     self.storage = storage  
  8.       
  9. obj= computer("hp","i5","4gp","1tb")#object name  
  10.   
  11. print(obj.computer_name)  
  12. print(obj.process)  
  13. print(obj.ram)  
  14. print(obj.storage)  
Output
 
Create an __init__ () function.
 
Python Classes And Objects
 

The self-parameter

 
The self-parameter refers to the current instance of the class. If you give any parameter you must use a self-keyword.
 
Example
  1. class student:#class  
  2.   def __init__(self, name,tamil,english,maths,social,science):# __init__ and self keyword  
  3.     self.name = name  
  4.     self.tamil = tamil  
  5.     self.english = english  
  6.     self.maths= maths  
  7.     self.social=social  
  8.     self.science= science  
  9.   
  10.   def total_mark(self):#self  
  11.     print("total mark ",end="")  
  12.     print("total mark ",  
  13. self.tamil+self.english+self.maths+self.social+self.science)  
  14.   
  15. obj =student("surya", 36,83,45,67,67)#object   
  16. obj1 =student("naveen", 36,67,85,57,97) 
  17. obj2=student("vijay", 56,83,75,47,87) 
  18. obj.total_mark()  
  19. obj1.total_mark()  
  20. obj2.total_mark() 
Output
 
Self-keyword usage.
 
Python Classes And Objects
 

Modify object properties

 
Modify objects are used to change object properties.
 
Example
  1. class car:#class  
  2.   def __init__(self, car_name, mil):  
  3.     self.car_name = car_name  
  4.     self.mil = mil  
  5.   def myfunc(self):  
  6.     print(self.car_name,self.mil)  
  7.   
  8. car1= car("Lamborghini", 20)#object  
  9. car2= car("ford",30)  
  10. car3= car("bmw", 40)  
  11.   
  12. car2.mil=28#modify object  
  13.   
  14. car1.myfunc()  
  15. car2.myfunc()  
  16. car3.myfunc() 
Output
 
Modify object.
 
Python Classes And Objects
 

Conclusion

 
In this article, we have seen Python classes and objects. I hope this article was useful to you. Thanks for reading!


Recommended Free Ebook
Similar Articles