Oops - Python

  • Methods
    • Procedural programming
    • Object-oriented programming
  • Class
    • Syntax
          class ClassName:
              # Constructor
              def __init__(self, value):
                  self.varName = value
              def functionName(self):
                  print(self.varName)
      
    • Constructor
          class ClassName:
              # Constructor
              def __init__(self, value):
                  self.varName = value
              # Alternative Constructor
              @classmethod
              def functionName(cls, varS):
                  return cls(varS.split("")[N])
          varName = ClassName("value")
          varName = ClassName.functionName("value")
      
  • Object
    • Syntax
          varName1 = ClassName(parameters)
          varName1.functionName()
          ClassName.functionName(varName1) # Converted to this above line
          print(varName1.varName)
          print(varName1.__dir__()) # Returns list of all methods that can be used
          print(varName1.__dict__) # Returns dictionary representation of object attributes
          print(help(varName1)) # Returns documentation for an object
      
    • Instance & Class Variables
          class ClassName:
              varName2 = value2 # Class Variable, Associated with Class
              def __init__(self, value):
                  self.varName = value
                  self.varName1 = value1 # Instance Variable, Associated with Object
              @classmethod
              def functionName(cls, value):
                  cls.varName2 = value # Changes class variable, First argument is instance of Class instead of Object
          varName = ClassName()
          varName.varName1 = value # Changing Instance variable
          varName.varName2 = value # Changing Class variable, Creates a instance variable
          varName.functionName(value) # Changing Class variable
          print(varName.varName2) # First searched for Instance Variable then Class Variable
      
  • Encapsulation
        class ClassName:
            # Getter
            @property
            def value(self):
                return self._value
            # Setter
            @value.setter
            def __init__(self, value):
                self._value = value
        varName = ClassName(parameters)
        print(varName.value)
    
  • Inheritance
    • Syntax
          class ClassName:
              # Parent Class
          class ClassName1(ClassName):
              # Child Class
          varName1 = ClassName1()
      
    • Types
      • Single
      • Multiple
      • Multi-level
      • Hierarchical
      • Hybrid
  • Polymorphism
  • Abstraction
    • Access Modifiers => Just a convention, not actually implemented
          # Public
          class ClassName:
              def __init__(self):
                  self.varName = value
              # Private
              def __init__(self):
                  self.__varName = value
              # Protected
              def __init__(self):
                  self._varName = value
          varName1 = ClassName()
          print(varName1.varName) # Access Public Variable
          print(varName1._ClassName__varName) # Access Private Variable, Name Mangling
          print(varName1._varName) # Access Protected Variable
      
    • Name Mangling
      • Technique used to protect class private and superclasses private attributes from being accidentally overwritten by subclass
  • Static Method
        # Public
        class ClassName:
            @staticmethod
            def functionName():
                # Statements
        ClassName.functionName(parameters)
    
Share: