WEB DEVELOPER SITE
PHP7
 

Python Object Oriented


Python is an item arranged language.This section causes you become full engineer utilizing Python's article situated programming support.

Introduction of OOP Terminology

Class − A client characterized model for an item that characterizes a lot of traits that portray any object of the class. The qualities are class factors and occasion factors and techniques, got to by means of dab documentation.
  • Class variable − A variable that is shared by all examples of a class. Class factors are characterized inside a class yet outside any of the class' techniques. Class factors are not utilized as every now and again as case factors are.

  • Data member − A class variable or occurrence variable that holds information related with a class and its objects.

  • Function overloading − The task of more than one conduct to a specific capacity. The activity performed shifts by the kinds of items or contentions involved.

  • Instance variable − A variable that is characterized inside a technique and has a place just with the present example of a class.

  • Inheritance − The exchange of the qualities of a class to different classes that are gotten from it.

  • Instance − An individual object of a specific class. An article obj that has a place with a class Circle, for instance, is an example of the class Circle.

  • Instantiation − The formation of an occurrence of a class.

  • Method − An exceptional sort of capacity that is characterized in a class definition.

  • Object − A special occurrence of an information structure that is characterized by its class. An item involves the two information individuals (class factors and occurrence factors) and methods.

  • Operator overloading − The task of more than one capacity to a specific operator.

    Creating Classes

    class proclamation makes another class definition.


    class ClassName: 
    
    'Discretionary class documentation string' 
    
    class_suite 
    
    
    • The class has a documentation string, which can be gotten to by means of ClassName.__doc__.

    • The class_suite comprises of all the part proclamations characterizing class individuals, information properties and functions.

    Creating Instance Objects

    To make occasions of a class, you call the class utilizing class name and go in whatever contentions its __init__ technique acknowledges.
    "This would make first object of Employee class" 
    
    emp1 = Employee("Maria", 1000) 
    
    "This would make second object of Employee class" 
    
    emp2 = Employee("Ava", 1200) 
    
    

    Accessing Attributes

    You get to the item's characteristics utilizing the speck administrator with article. Class variable would be gotten to utilizing class name as pursues −

    emp1.displayEmployee() 
    
    emp2.displayEmployee() 
    
    print "Absolute Employee %d" % Employee.empCount 
    
    
    Presently, assembling every one of the ideas
     
    
    #!/usr/container/python 
    
    class Employee: 
    
    'Regular base class for all representatives' 
    
    empCount = 0 
    
    def __init__(self, name, pay): 
    
    self.name = name 
    
    self.salary = pay 
    
    Employee.empCount += 1 
    
    def displayCount(self): 
    
    print "Absolute Employee %d" % Employee.empCount 
    
    def displayEmployee(self): 
    
    print "Name : ", self.name, ", Salary: ", self.salary 
    
    "This would make first object of Employee class" 
    
    emp1 = Employee("Maria", 1000) 
    
    "This would make second object of Employee class" 
    
    emp2 = Employee("Ava", 1200) 
    
    emp1.displayEmployee() 
    
    emp2.displayEmployee() 
    
    print "All out Employee %d" % Employee.empCount 
    
    
    At the point when the above code is executed, it delivers the accompanying outcome
     
    
    $python main.py 
    
    Name : Maria , Salary: 1000 
    
    Name : Ava , Salary: 1200 
    
    All out Employee 2 
    
    
    Run Example »

    You can include, evacuate, or adjust qualities of classes and items whenever −

    emp1.age = 7 # Add an 'age' characteristic. 
    
    emp1.age = 8 # Modify 'age' characteristic. 
    
    del emp1.age # Delete 'age' characteristic. 
    
    

    Instead of utilizing the typical articulations to get to qualities, you can utilize the accompanying capacities −

    • The getattr(obj, name[, default]) − to get to the characteristic of object.

    • The hasattr(obj,name) − to check if a quality exists or not.

    • The setattr(obj,name,value) − to set a trait. On the off chance that quality does not exist, at that point it would be created.

    • The delattr(obj, name) − to erase an attribute.

    hasattr(emp1, 'age') # Returns genuine if 'age' property exists 
    
    getattr(emp1, 'age') # Returns estimation of 'age' characteristic 
    
    setattr(emp1, 'age', 8) # Set characteristic 'age' at 8 
    
    delattr(empl, 'age') # Delete characteristic 'age' 
    
    

    Built-In Class Attributes

    Every Python class continues following implicit characteristics and they can be gotten to utilizing spot administrator like some other quality −

    • __dict__ − Dictionary containing the class' namespace.

    • __doc__ − Class documentation string or none, if vague.

    • __name__ − Class name.

    • __module__ − Module name in which the class is characterized. This characteristic is "__main__" in intuitive mode.

    • __bases__ − A conceivably vacant tuple containing the base classes, in the request of their event in the base class list.

      For the above class let us attempt to get to every one of these traits
       
      
      #!/usr/receptacle/python 
      
      class Teacher: 
      
      'Basic base class for all Teachers' 
      
      empCount = 0 
      
      def __init__(self, name, compensation): 
      
      self.name = name 
      
      self.salary = compensation 
      
      Teacher.empCount += 1 
      
      def displayCount(self): 
      
      print "All out Teacher %d" % Teacher.empCount 
      
      def displayTeacher(self): 
      
      print "Name : ", self.name, ", Salary: ", self.salary 
      
      print "Teacher.__doc__:", Teacher.__doc__ 
      
      print "Teacher.__name__:", Teacher.__name__ 
      
      print "Teacher.__module__:", Teacher.__module__ 
      
      print "Teacher.__bases__:", Teacher.__bases__ 
      
      print "Teacher.__dict__:", Teacher.__dict__ 
      
      
      At the point when the above code is executed, it delivers the accompanying outcome
       
      
      $python main.py 
      
      Teacher.__doc__: Common base class for all Teachers 
      
      Teacher.__name__: Teacher 
      
      Teacher.__module__: __main__ 
      
      Teacher.__bases__: () 
      
      Teacher.__dict__: {'__module__': '__main__', 'displayCount': , 'empCount': 0, 'displayTeacher': , '__doc__': 'Basic base class for all Teachers', '__init__': } 
      
      Run Example » 
      
      

      Destroying Objects (Garbage Collection)

      The procedure by which Python occasionally recovers squares of memory that never again are being used is named Garbage Collection. The article's reference check diminishes when it's erased with del, its reference is reassigned, or its reference leaves scope. At the point when an article's reference tally achieves zero, Python gathers it naturally.

      Example

      This __del__() destructor prints the class name of an example that is going to be annihilated

       
      
      #!/usr/receptacle/python 
      
      class Point: 
      
      def __init__( self, x=0, y=0): 
      
      self.x = x 
      
      self.y = y 
      
      def __del__(self): 
      
      class_name = self.__class__.__name__ 
      
      print class_name, "devastated" 
      
      X = Point() 
      
      Y = X 
      
      Z = X 
      
      print id(X), id(Y), id(Z) # prints the ids of the obejcts 
      
      del X 
      
      del Y 
      
      del Z 
      
      
      At the point when the above code is executed, it produces following outcome
       
      
      $python main.py 
      
      140158590223064 
      
      Point wrecked 
      
      
      Run Example »

      Class Inheritance

      Rather than beginning without any preparation, you can make a class by getting it from a prior class by posting the parent class in enclosures after the new class name.

      Syntax

      Derived classes are pronounced much like their parent class; notwithstanding, a rundown of base classes to acquire from is given after the class name −

      class SubClassName (ParentClass1[, ParentClass2, ...]): 
      
      'Optional class documentation string' 
      
      class_suite