Python Classes and Objects is an essential part of Python Programming Language.In Python, everything is an object, an instance of some Class.So Python Classes And Objects provides all the feature of Object Oriented Programming Language.
Python Classes And Objects
What is Classes:
The Class is an important part of Python Programming, as it provides all the concept of object oriented nature.So classes are nothing but the collection of some Attributes and Functions or Method.It is somewhat similar to Structs in C language, but more powerful and easy to understand.
What is Object:
An Object is an instance of Class.A unique instance of a data structure that’s defined by its class. An object comprises both data members (class variables and instance variables) and methods.
So How to create a class.Here is a simple example
class ClassName: #Creating class with keyword "class" followed by the class name Some Attributes Some Attributes obj=ClassName() # Creating Object "obj" to access class Attributes and Attributes
Let Create Some Example
class Employe: def __init__(self, firstName, lastName, country, phone, email): self.firstName = firstName self.lastName = lastName self.country = country self.telephone = phone self.email = email def getEmp(): print "The Employe %s %s From %s with Phone no %d with Email id %s is working in PythonLover.net"%(self.firstName,self.lastName, self.address = address,self.telephone = phone,self.email = email) emp1=Employe("Python","Lovers","India","0000000000","firstname.lastname@example.org") emp1.getEmp() emp2=Employe("Java","Lovers","India","1100000000","email@example.com") emp2.getEmp()
What we have done here is we create a class ” Employe “, then inside that class, we create a method ” __init__() ” with some arguments i.e (self, firstName, lastName, address, phone, email).then we assign those value to a variable using “self” keyword [Note: you can assign it to any variable name like ” self.name=firstname “,” self.surname=lastname “]
self.firstName = firstName
self.lastName = lastName
self.country = country
self.telephone = phone
self.email = email
Nowhere “__init__()” method act like a Constructor,as we define in C++,Java.It’s used to initialize value OR execute specific code that we want to execute at the time when we create an object.when the object is created it automatically call ” __init__()” method and execute the code inside it.
Now a question comes in your mind that, what is the use of ” self ” keyword here.In simple word “self” keyword identify object that calls the class method or attribute and assigns or performs or executes code only for that Object
In our example we create two objects i.e “emp1” and “emp2”.when object is created and when we call method by using object, “self” keyword identify which object is calling method and assign values to that particular object
Now when we execute the code we get the fallowing output
The Employe Python Lovers From India with Phone no 0000000000 with Email id firstname.lastname@example.org is working in PythonLover.net The Employe Java Lover From India with Phone no 1100000000 with Email id email@example.com is working in PythonLover.net
Class Inheritance means Extending the property of one class i.e “Parent Class” to another class i.e “Child Class”.Suppose there is the class called “A ” that have a method add().Now there is another class called “B” that need same functionality that adds() method have in class “A”.So instead of writing the same method with the same functionality we Inherit the class “A” to class “B”.so that we can save rewriting of same code and time also.
So the general syntax for Class Inheritance is given in below image
Here is a simple example of Inheritance
class Parent: # define parent class p = "Thanks for calling parent class constructor" def __init__(self): print "Initialising and %s"%(self.p) def add(self,a,b): self.a=a self.b=b print 'Calling parent method from Child object' sum=(self.a+self.b) print "Addition : %d"%(sum) class Child(Parent): #extending parent class # define child class c = "Thanks for calling Child class constructor" def __init__(self): print "Initialising and %s"%(self.c) def subtract(self,a,b): self.a=a self.b=b print 'Calling parent method from Child object' sub=(self.a-self.b) print "Subtration : %d"%(sub) pra = Parent() chi = Child() chi.add(10,10) #calling parent add() method using Child class object chi.subtract(20,10)
The output of above code is:
Initialising and Thanks for calling parent class constructor Initialising and Thanks for calling Child class constructor Calling parent method from Child object Addition : 20 Calling add() method of Parent class from Child object Subtraction: 10
Similarly, you can inherit multiple classes or you can drive a class from multiple parent classes
class A: ..... # define your class A class B: ..... # define your class B class C(A, B): ..... # subclass of A and B
You can perform all the function as we do in C++ like Method Overriding, Operator overloading etc. For more detail on Classes and Object please visit https://docs.python.org/2/tutorial/classes.html
Hope guys you will understand the concept of Python Classes and Object clearly.Try to implement it with more code, and if you find any difficulty, feel free to drop a comment on this post.