Python Object-Oriented

Python is also known as a high-level programming language with a multi-paradigm, one of those paradigms is Python Object-Oriented Programming.

Object-oriented programming is a programming technique that most high-level programming languages support. In this class and objects are used to make a program.

Here are some terminologies which are used in Python object-oriented programing described below,

Class:

A class is a Keyword that is used to make the blueprint of an Object.  A class has no existence until its object gets created.

Class variable:

A variable is that shared by all instances of a class. Class variables are defined within a class but outside any of the class’s methods. Instance variables are used more frequentlly asare than Class variables.

Instance:

Instance is known as 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:

 A variable that is defined inside a method and belongs only to the current instance of a class is known as instance variable

Inheritance:

Inheritance is the transfer of the characteristics of a class to other classes that are derived from it. Through which it can use the properties of one class in another. 

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.

Instantiation:

The creation of an instance of a class in python is called Instantiation .

Object:

 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.

Method:

 A function that is defined in a class definition.

Operator overloading:

The assignment of more than one function to a particular operator.

Data member:

A class variable or instance variable that holds data associated with a class and its objects.

Now let’s have a look on these terminologies in details,

Classes:

A class is a user-defined blueprint or prototype from which objects are created. A class has no existence until its object gets created.

Syntax:

Creating Instance Objects:

Accessing Attributes:

#creating class
class Student:
   'Common base class for all Student'
   studCount = 0
   def __init__(self ,id, name):
       self.id = id
       self.name = name
       Student.studCount += 1
   
   def displayCount(self):
     print ("Total Students %d" %  Student.studCount)
   def displayStudent(self):
      print  ("ID: ", self.id ,  "Name : ", self.name) 
"This will create first object of Student class"
stud1 = Student(13 , "Alice")
"This would create second object of Employee class"
stud2 = Student(10 , "Bob")
stud1.displayStudent()
stud2.displayStudent()
print ("Total Student %d" % Student.studCount)

Built-In Class Attributes:

Python class follow built-in attributes and can be accessed using dot operator like any other attribute,

  1. name − Define Class name.
  2. module − In the Module, we define the class.
  3. dict − Dictionary containing the class’s namespace.
  4. doc − Class documentation string or none, if undefined.
  5. bases − A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

Let us perform all these on above mentioned example to access all attributes in class,

#creating class
class Student:
   'Common base class for all Student'
   studCount = 0
   def __init__(self ,id, name):
       self.id = id
       self.name = name
       Student.studCount += 1
   
   def displayCount(self):
     print ("Total Students %d" %  Student.studCount)
   def displayStudent(self):
      print  ("ID: ", self.id ,  "Name : ", self.name, )
"This will create first object of Student class"
stud1 = Student(13 , "Alice")
"This would create second object of Employee class"
stud2 = Student(10 , "Bob")
stud1.displayStudent()
stud2.displayStudent()
print ("Total Student %d" % Student.studCount)
print ("Student.__doc__:", Student.__doc__)
print ("Student.__name__:", Student.__name__)
print ("Student.__module__:", Student.__module__)
print ("Student.__bases__:",Student.__bases__)
print ("Student.__dict__:", Student.__dict__)

Class Inheritance:

In python, Class Inheritance enables us to define a class that takes all the functionality from a parent class and allows us to add more functionality to it.

Syntax:
#class inheritance

class Parent:        # define parent class
   parentAttribute = 50
   def __init__(self):
      print ("Call Parent Constructor")

   def Methodparent(self):
      print ("Call Parent Method")

   def setAttribute(self, attribute):
      Parent.parentAttribute = attribute

   def getAttribute(self):
      print ("Parent Attribute :", Parent.parentAttribute)

class Child(Parent): # defining child class
   def __init__(self):
      print ("Call Child Constructor")

   def Methodchild(self):
      print ('Call Child Method')

c = Child()          # instance of child method
c.Methodchild()      # child method calls its method
c.Methodparent()     # calls parent method
c.setAttribute(100)       # again calling parent's method
c.getAttribute()          # again calling parent's method

Overriding Methods:

Parent class methods can also be overridden. If you want some different functionality in your subclass then you’ll override the parent’s methods.

Understand this with the help of example given below,

#python program for overriding
class ParentClass:        #defining parent class
   def ParentMethod(self):
      print ("Parent method is called")
class ChildClass(ParentClass): #defining child class
   def ChildMethod(self):
      print ("Child method is called")
x = ChildClass()          #instance of child method
x.ChildMethod()         #child method calls overridden method

Base Overloading Methods:

Following table is the lists of some generic functionality that can override in classes.

Methods DescriptionSyntax
__del__( self )The Destructor deletes an object del obj
__init__ ( self [,args…] )The Constructor with any optional arguments obj = className(args)
__repr__( self )Representation of Evaluable string  repr(obj)
__cmp__ ( self, x )Comparison Object cmp(obj, x)
__str__( self )Representation of Printable string
str(obj)

Overloading Operators:

In Python sometimes we have to represent the two-dimensional vectors, to plus these vectors there is a method that is used in a class named _add_ method.

Take a look on this example,

#overriding operators

class parent:
   def __init__(self, x, y):
      self.x = x
      self.y = y

   def __str__(self):
      return 'parent (%d, %d)' % (self.x, self.y)
   
   def __add__(self,other):
      return parent(self.x + other.x, self.y + other.y)

v1 = parent(8,12)
v2 = parent(20,9)
print (v1 + v2)

Data Hiding:

An object’s properties/attributes can or can’t be obvious outside the class definition. You would like to title/name properties/attributes with a double underscore prefix, and those properties at that point are not directly visible to outsiders.

See Following example will help you to have better understanding:

#data hidding program
class parentCount:
   __childCount = 0
  
   def count1(self):
      self.__childCount += 5
      print (self.__childCount)
count2 = parentCount()
count2.count1()
count2.count1()
print (count2.__childCount)