Python II # OOPS – Part 2

All the example here may be or may not be connected to each other depending on the need. Be wise & understand where & how you can use this functionality for your need.

Getter and Setter Method

class Square:
    def __init__(self, height=0, w

    # getter
    @property
    def height(self):
        print(“retriving the height”)
        return self.__height

    # setter
    @height.setter
    def height(self, value):
        if value.isdigit():
            self.__height = value
        else:
            print(‘InputError: only numerical value pace’)

Note: Check the setter and getter method added for Square class

  • Actual value if height is stored in another variable called __height
  • Keyword @property for getter method
  • Keyword #.setter for setter method

Methods – class methods

@staticmethod
def whoami():
    print(“awesome”)

Class Methods: It does not matter if class is initialised, it just works.

Note:

  • keyword to note is staticmethod
  • Inside params there is no self

Magic Method

def __str__(self):
    return “know thyself”, self.__name__, self.health, self.hp

Note: This method gives a meaning result when you print this object otherwise you will only see an instance’s meta data which you won’t understand.

Inheritance

class Mammal(Animal):
    def __init__(self, birthType=”born alive”,…):
        Animal.__init(self, birthType, appearance,…._:
        self.__new_features = new_feature

    @property
    def new_features():
        return self.__nurseYoung

    @new_feature.setter
    def nurseYoung(self, nurseYpung):
        if nurseYoung.isalpha():
            self.__nurseYOung = nurseYOung
        else:
            print “nurseYOung has to be a string”

Note: Look at is init method

  • We used parentheses in the Class Name as we using inheritance – otherwise not required.
  • This class called its parent class Animal & then does the initialisation using Animal’s init and then
  • For extra variables added variable with its custom getter & setter methods

overriding

    def __str__(self):
        ## appending super’s output & printing a new info
        return super().__str__() + “ and also nurse their young”

Note: This method is to call its parent & use its out and then makes its own new with added information.

  • Parent already has that method.

Function Overload / Operator Overload ==> no. of input variables is not fixed

# In Python - type casting setting is Dynamic
# In Java, C - its static Type Casting
##  def sumAll(self, a_int, b_int, c_int)
##  def sumAll(self, a_int, b_int, c_str)
##  def sumAll(self, a_int, b_str, c_str)

def sumAll(self, *args):
    sum = 0
    for i in args:
        sum += int(i)
    return sum

Note: This method works for any number of inputs number* whether given as number or strings

polymorphism

def tell_me_whp_are_you(object):
    print(‘my name is’, object.__name__)

Note: This method works for any object/instance that has name attribute, it does not matter what kind of object it is.

Magic method (part2) (Already shown once above for str)

def __add__(vector1, vector2):
    vector1.x += vector2.x
    vector1.y += vector2.y
    vector1.z += vector2.z
    vector2 = vector1 # repoint & del vector2
    return vector1

Note: Like init, this add can help you create basic functions like add, sub, multiplayer, div, mod-modulus.

Advertisements