Python Essential Classroom Series – 31/Jul/2021

Objects and Classes

  • Any object has capabilities (behaviours) and contents (data)
  • Examples-1 : SavingsAccount
behaviors:
  deposit
  withdraw
  transfer
data:
  name
  accountno
  balance
  • Examples-2: Product
behaviors:
  bought
  sold
data:
   name
   brand
   category
   price
  • Example-3: Patient
behavior:
  tests
  consultation
data:
  mrn_id
  address
  phone_number
  diagnostic_reports
  • Class is a design for an object. It is also referred as Blue print which can be used to create multiple objects
  • Object represents real world. We create an object using class which is referred as instantiation.
  • When we write code we create classes to design or to model some part of application and we create objects to work with models
  • Simple Class and object Creation
class Number:
    """
    This class represents a number
    """

    def is_prime(self):
        """
        This method is used to find if the number is prime or Not
        """
        pass

    def is_even(self) -> bool:
        """
        This method is used to find if the number is even or not
        """
        pass

number1 = Number() #creating object or instantiation
number2 = Number() #creating object or instantiation
  • Class contains members and methods. Methods in a class are of 3 types
    • instance methods
    • class methods
    • static methods
  • While creating an object from a class if you want to initialize some values we can use __init__. In python any methods or members with double underscores (dunder) around it are special and they are designed for a specific usage by python.
  • Each object has its own memory locations
  • The instance methods will have the first argument as self which represent the object of the current class.
class Number:
    """
    This class represents a number
    """
    def __init__(self, number: int) -> None:
        self.number = number
        

    def is_prime(self):
        """
        This method is used to find if the number is prime or Not
        """
        pass

    def is_even(self) -> bool:
        """
        This method is used to find if the number is even or not
        """
        return self.number%2 == 0

number1 = Number(4) #creating object or instantiation
number2 = Number(number=5) #creating object or instantiation

number1.is_even() # we write code using this
Number.is_even(number1) # This is how it actually gets called

  • Sample Calculator
class Calculator:
    def __init__(self) -> None:
        self.memory = list()

    def add(self, *args):
        result = 0
        for number in args:
            result += number
        self.append_to_memory(result)
        return result

    def multiply(self, *args):
        result = 1
        for number in args:
            result *= number
        self.append_to_memory(result)
        return result
    
    def sub(self, number1, number2):
        result = number1 - number2
        self.append_to_memory(result)
        return result

    def div(self, number1, number2):
        result = number1/number2
        self.append_to_memory(result)
        return 

    def append_to_memory(self,result):
        self.memory.append(result)
            
    def memory_reset(self):
        self.memory.clear()

    def memory_op(self, index):
        return self.memory[index]


calc = Calculator()
calc.add(1,2,3,4,5)
calc.sub(5,1)
calc.div(4,2)
calc.multiply(1,2,3)
print(calc.memory_op(0))
print(calc.memory_op(-1))
    

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin