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.
- 1 Python Classes and Objects
- 1.1 Overview of OOPs in Python
- 1.2 Python Class
- 1.3 Python Object
- 1.4 Class variable
- 1.5 Python Function overloading
- 1.6 Inheritance in Python
- 1.7 Python Instance
- 1.8 Instance variable in Python
- 1.9 Method
- 1.10 Operator overloading in Python
- 1.11 Create a Class
- 1.12 Object creation
- 1.13 Create Instance Objects
- 1.14 Accessing Attributes
- 1.15 Class Inheritance in Python
- 1.16 Creating child class in Python
- 1.17 Destroying Objects in Python
- 1.18 Overriding Methods in Python
- 1.19 Overloading Methods in Python
- 1.20 _init_ ( self[,args…] )
- 1.21 _del_(self)
- 1.22 _repr_(self)
- 1.23 _str_(self)
- 1.24 _cmp_(self,x)
- 1.25 Overloading Operators in Python
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.
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.
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;
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.
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.
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
class MyClass: x = 2
p1 = MyClass() Print (p1.x)
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.
emp1 = Employee ("Zara", 2000) emp2 = Employee ("Manni", 5000)
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:
emp1.displayEmployee() emp2.displayEmployee() print "Total Employee %d" % Employee.empCount
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
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.
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
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”.
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  = -1 #Decrease ref. count of <20>
Overriding Methods in Python
You always have a chance to overriding parent class methods.
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
Calling child method
Overloading Methods in Python
_init_ ( self[,args…] )
Constructor (with any optional arguments)
sample call: obj = className(args)
destructor deletes an object
sample call: del obj
evaluable string representation
printable string representation
sample call: str(obj)
sample call: cmp(obj, x)
Overloading Operators in Python
It can change the function of some specific operators to do some different tasks.
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