Thursday, July 29, 2021

Python Object Oriented Programming Jumpstart

Python Object Oriented Programming Jumpstart



This is the quick and dirty jumpstart object oriented programming for Python. Just for quick start reference.

A simple object

$ edit calculator.py
  
class Calculator(object):
  def plus(self, a,b):  # self refer to the obj itself
	return a+b 

print(Calculator().plus(20,30))
cal= Calculator()
print(cal.plus(2,7))
⇒ 50
⇒ 9

The object in Calculator(object) is just a object variable, it is not used here and will be explained later.
The self in plus(self, a,b) is always there, it is a object variable refer to the object itself, will be explained later. If there is no parameter required in the function just declare f(self) will do.
You can use the Calculator() class itself, or use the cal object created from Calculator() class, eg. Calculator().plus(), cal.plus()

It can be:

class Calculator(object):
  pass
  

There is no implementation in the Calculator() class.


Format: old and new


# old
class Calculator:
  pass

# new
class Greeting(object):  # or class Greeting():
  def hello(self):
    print('Hello, world')

Note:
'pass' means there is not implementation in Calculator() class.


Python: constructor

Constructor use to initialize object.

class Greeting(object):
    def __init__(self):
        print('initialize')
        
    def hello(self):
        print('Hello, world')

a= Greeting()  # ⇒ initialize
a.hello()  # ⇒ Hello, world
  

Python: Inheritance

Python allow multipl inheritance, not discuss here. eg. class obj(parent1, parent2)


class Calculator(object):
  name= 'Calculator'
  def plus(self, a,b):  # self refer to the obj itself
	return a+b 

# inheritance
class ScientificCalculator(Calculator):  # inherit from Calculator
  pi= 3.14
  def circle_area(self, r):
    return  self.pi * r * r

sc= ScientificCalculator()
print(sc.plus(sc.pi,2))
print(sc.circle_area(3))
⇒ 5.14
⇒ 28.26

Scientific calculator(sc) inherit from Calculator() class. The function plus() is not in scientific calculator, but it will be inherited from the parent class(Calculator()).
If you implement a new plus() in Scientific Calculator, it will overwrite the plus() in Calculator() class.
The self.pi refer the pi variable in the local class object, for parent class object use super().name (or Calculator().name)
Try to access pi without self.pi result error, 'name pi is not defined'.


The different between Calculator() and cal object

Calculator().name= 'Scientific Calculator'
cal= Calculator()
cal.name= 'Canon Calculator'

print(Calculator().name)  # ⇒ 'Calculator'
print(cal.name)  # ⇒ 'Canon Calculator'

The Calculator() code follow the previous code.
The cal object name variable change to 'Canon Calculator' but the class (Calculator()) name remain the same.


Another example: Bird

class Bird(object):
    def swing(self):
        print('swing wing')
    def fly(self):
        print('bird fly')

class Penguin(Bird):
    def fly(self):
        print('Sorry, penguin can not fly')

bird= Bird()
bird.fly()
tux= Penguin()
tux.fly()

The Penguin() class inherite from Bird() class. The Penguin().fly() overwrite the Bird.fly(), so penguin can not fly.