Basic Python

super, classmethod, abstractmethod, entity

Naranjito 2021. 5. 24. 19:33
  • super() : It allows you to use it in the superclass without repeating code. 
class Rectangle:
    def __init__(self, length, width, **kwargs):
        self.length = length
        self.width = width
        super().__init__(**kwargs)

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from 
# the Rectangle class
class Square(Rectangle):
    def __init__(self, length, **kwargs):
        super().__init__(length=length, width=length, **kwargs)

class Cube(Square):
    def surface_area(self):
        face_area = super().area()
        return face_area * 6

    def volume(self):
        face_area = super().area()
        return face_area * self.length

class Triangle:
    def __init__(self, base, height, **kwargs):
        self.base = base
        self.height = height
        super().__init__(**kwargs)

    def tri_area(self):
        return 0.5 * self.base * self.height

class RightPyramid(Square, Triangle):
    def __init__(self, base, slant_height, **kwargs):
        self.base = base
        self.slant_height = slant_height
        kwargs["height"] = slant_height
        kwargs["length"] = base
        super().__init__(base=base, **kwargs)

    def area(self):
        base_area = super().area()
        perimeter = super().perimeter()
        return 0.5 * perimeter * self.slant_height + base_area

    def area_2(self):
        base_area = super().area()
        triangle_area = super().tri_area()
        return triangle_area * 4 + base_area

reference : realpython.com/python-super/

 

  • @classmethod

It is used to declare a method in the class as a class method that can be called using ClassName.MethodName(). The class method can also be called using an object of the class. The first parameter must be cls, which can be used to access class attributes. It can only access the class attributes but not the instance attributes.

import random
class Account:
    account_num=0
    def __init__(self, name, balance):
        self.name=name 
        self.balance=balance
        self.bank='SC'
        num1=random.randint(0,999)
        num2=random.randint(0,99)
        num3=random.randint(0,999999)

        num1=str(num1).zfill(3)
        num2=str(num2).zfill(2)
        num3=str(num3).zfill(6)

        self.num=num1+'-'+num2+'-'+num3

        Account.account_num+=1

    @classmethod
    def get_account_num(cls):
        print(cls.account_num)

reference : https://www.tutorialsteacher.com/python/classmethod-decorator#:~:text=In%20Python%2C%20the%20%40classmethod%20decorator,of%20the%20classmethod()%20function.

 

  • @abstractmethod

It defines essential method that sub class need to implement. It cannot be called so defines empty method, meaning it cannot create instance. It needs only for inheritance.

from abc import * 

class StudentBase(metaclass=ABCMeta):
    @abstractclassmethod 
    def study(self):
        pass 

    @abstractclassmethod 
    def go_to_school(self):
        pass 

class Student(StudentBase):
    def study(self):
        print('study')

    def go_to_school(self):
        print('school')
        
a=Student()
a.study()
a.go_to_school()
>>>study
>>>school

 

  • entity

It is class which stores an object's state in the database.

class Parrot:
    def fly(self):
        print('parrot fly')

class Airplane:
    def fly(self):
        print('plane fly')

class Whale:
    def swim(self):
        print('whale swim')

def lift(entity):
    entity.fly()

p=Parrot()
a=Airplane()
w=Whale()

lift(p)
lift(a)
lift(w)
>>>
parrot fly
>>>
plane fly
>>>
AttributeError: 'Whale' object has no attribute 'fly'