Python classes and objects – OOPs in Python: As we all know python is an object-oriented programming language. Almost everything in python refers to an object. Let us know in brief regarding python classes and objects.
Python Classes and Objects
Overview of OOPs in Python
There are different concepts in OOPs (Object Oriented Programming). They are Class, Object, Inheritance, Instance, Function Overloading, Operator Overloading, etc.
Python Class
It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
Python Object
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
Syntax: Class Name Object Name;
Class variable
A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class’s methods. Class variables are not used as frequently as instance variables are.
Python Function overloading
The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved.
Inheritance in Python
The transfer of the characteristics of a class to other classes that are derived from it.
Python Instance
An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
Instance variable in Python
A variable that is defined inside a method and belongs only to the current instance of a class.
Method
A special kind of function that is defined in a class definition.
Operator overloading in Python
The assignment of more than one function to a particular operator.
Create a Class
Example
class MyClass: x = 2
Output:<class ‘__main__.MyClass’>
Object creation
Example
p1 = MyClass() Print (p1.x)
Output: 2
Create Instance Objects
If you want to create an instance of a class, you will call the class using the class name and pass the arguments _init method.
Example
emp1 = Employee ("Zara", 2000) emp2 = Employee ("Manni", 5000)
Accessing Attributes
Attributes can access the object’s attributes using the dot operator with an object. A class variable would be accessed using a class as follows:
Example
emp1.displayEmployee() emp2.displayEmployee() print "Total Employee %d" % Employee.empCount
Example
class Employee:'Common base class for all employees' empCount = 0 def __init_(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name: ", self.name, ", Salary: ", self.salary emp1 = Employee ("Joseph", 2000) #"This would create first object of Employee class" emp2 = Employee ("Meera", 4000) #"This would create second object of Employee class" emp1.displayEmployee() emp2.displayEmployee() print "Total Employee %d" % Employee.empCount
Output
Name: Joseph, Salary:2000
Name Meera, Salary:4000
Total Employee 2
Class Inheritance in Python
It allows us to define a class that inherits all the methods and properties of another class. The parent class is the class being inherited from, also called the base class. The child class is the class that inherits from another class, also called derived class.
Example
Parent Class: class Person: def __init_(self, fname, lname): self.firstname = fname self.lastname = lname def printname(self): print(self.firstname, self.lastname) #Use the Person class to create an object, and then execute the printname method: x = Person ("Rosy", "Novel") x.printname()
Output: Rosy Novel
Note: Use the pass keyword when you do not want to add any other properties or methods to the class.
Creating child class in Python
Example
x = Student ("Mork", "Stones") x.printname()
Output: Mork Stones
Destroying Objects in Python
Python deleted the unwanted objects automatically, to free memory space. A block of memory that no longer used can be deleted by “Garbage Collector”.
Example
a = 20 #Create object <20> b = a #Increase ref. count of <20> c = [b] #Increase ref. count of <20> del a #Decrease ref. count of <20> b = 50 #Decrease ref. count of <20> c [0] = -1 #Decrease ref. count of <20>
Overriding Methods in Python
You always have a chance to overriding parent class methods.
Example
class Parent: # define parent class def myMethod(self):print 'Calling parent method' class Child (Parent): #define child class def myMethod(self): print 'Calling child method' c = Child () #an instance of child c.myMethod() #child calls overridden method
Output
Calling child method
Overloading Methods in Python
_init_ ( self[,args…] )
Constructor (with any optional arguments)
sample call: obj = className(args)
_del_(self)
destructor deletes an object
sample call: del obj
_repr_(self)
evaluable string representation
sample call:repr(obj)
_str_(self)
printable string representation
sample call: str(obj)
_cmp_(self,x)
object comparison
sample call: cmp(obj, x)
Overloading Operators in Python
It can change the function of some specific operators to do some different tasks.
Example
class Vector: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return 'Vector (%d, %d)' % (self.x, self.y) def __add_(self, other): return Vector(self.x + other.x, self.y + other.y) v1 = Vector (2,5) v2 = Vector (3, -2) print v1 + v2
Output
vector (5,3)