Python basics ⑤

Basic knowledge of Python ⑤. It is my study memo. Please do not excessive expectations.

Past posts

Python basics Python basics ② Python basics ③ Python basics ④

Inheritance

-Create a new class based on an existing class Inherit other classes by setting " class new class name (original class name): " You can define a new class. The new class is called the "child class" </ b> and the original class is called the "parent class" </ b>.

menu_item.py


#Parent class
class MenuItem:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def info(self):
        return self.name + ': ¥' + str(self.price)

    def get_total_price(self, count):
        total_price = self.price * count

        if count >= 3:
            total_price *= 0.9

        return round(total_price)

sweets.py


#Child class
# menu_item.Load MenuItem class from py
from menu_item import MenuItem

#Define a new class by inheriting another class
class Sweets(MenuItem):
    pass #Indicates that there is no processing
    #Align the indents (4 half-width spaces)

-Instance method of child class The child class can use both "methods defined in the parent class" and "methods defined independently". However, the methods of the child class cannot be used in the parent class.

sweets.py


#Child class
from menu_item import MenuItem

class Sweets(MenuItem):
    def calorie_info(self): #Add self to the first argument in the method definition
        print(str(self.calorie) + 'is kcal') # str()Convert numbers to strings with
        #Align the indents (4 half-width spaces)

-override Defining a method in a child class with the same name as a method in the parent class overrides the method. In short, the method defined in the child class is called with priority.

menu_item.py


#Parent class
class MenuItem:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def info(self):
        return self.name + ': ¥' + str(self.price)

    def get_total_price(self, count):
        total_price = self.price * count

        if count >= 3:
            total_price *= 0.9

        return round(total_price)

sweets.py


from menu_item import MenuItem

class Food(MenuItem):
    #define info method
    def info(self):
        return self.name + ': ¥' + str(self.price) + str(self.calorie) + 'kcal'
    
    def calorie_info(self):
        print(str(self.calorie) + 'is kcal')

The info method in the child class takes precedence and is overwritten instead of the info method in the parent class

Duplicate in method

-By setting "super (). method name ()" ` The instance method defined in the parent class can be used as it is.

menu_item.py


#Parent class
class MenuItem:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def info(self):
        return self.name + ': ¥' + str(self.price)

    def get_total_price(self, count):
        total_price = self.price * count

        if count >= 3:
            total_price *= 0.9

        return round(total_price)

sweets.py


from menu_item import MenuItem

class Food(MenuItem):
    def __init__(self, name, price):
        # super()Of the parent class using__init__()Call
        super().__init__(name, price)
        # self.name =name → Delete the duplicated part
        # self.price =price → Delete the duplicated part
    
    def info(self):
        return self.name + ': ¥' + str(self.price) + str(self.calorie) + 'kcal'

    def calorie_info(self):
        print(str(self.calorie) + 'is kcal')

Recommended Posts