[Python] Class inheritance, override

Create a new class based on a certain class

** class New class name (original class name): **

from menu_item import MenuItem

class Food(MenuItem):
      pass

override

Description You can overwrite the contents by defining a method with the same name as the parent class in the child class. When a method is called, the overwritten method is called.

** Example sentence ** Partial excerpt of the method part (overriding the info method) Parent class (already has a method)

menu_item.py


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

Override in child class

food.py


from menu_item import MenuItem
class Food(MenuItem):
   def info(self):
       return self.name + ': ¥' + str(self.price) + ' (' + str(self.calorie) + 'kcal)'

Recommended Posts

[Python] Class inheritance, override
[Python] Class inheritance (super)
Class inheritance
Python #inheritance (inheritance)
[Python of Hikari-] Chapter 09-03 Class (inheritance)
[Python] class, instance
About python, class
About python inheritance
Python class, instance
#Python basics (class)
Python class (Python learning memo ⑦)
case class in python
Try to solve the Python class inheritance problem
Python self-made class sort
[python] class basic methods
python subprocess wrapper class
Trouble with Python pseudo-private variables and class inheritance
Class inheritance practice in python as seen in sklearn
YOLO Python wrapper class
Class notation in Python
Python exception class list
Python: Class and instance variables
C / C ++ programmer challenges Python (class)
Python class member scope summary
Python class variables and instance variables
[python] super (), inheritance, __init__, etc.
Class inheritance and super functions
I'll inherit and override the class
How to write a Python class
perl objects and python class part 2.
Python
Landmines hidden in Python class variables
Python class definitions and instance handling
"The easiest Python introductory class" modified
I looked into class inheritance overrides.
Read PNG chunks in Python (class)
class
[Python] Road to snake charmer (3) Python class
Examine the object's class in python
perl objects and python class part 1.
class
[Python] Inherit a class with class variables
Understanding python classes Struggle (2) Work That's not all, right? _ About class inheritance
Generate a first class collection in Python
[Python] Extension using inheritance of matplotlib (NavigationToolbar2TK)
Create a Python function decorator with Class
[Introduction to Python] How to use class in Python?
[Python] What is inherited by multiple inheritance?
Implement __eq__ etc. generically in Python class
[Learning memo] Basics of class by python
Build a blockchain with Python ① Create a class
Python: [Inheritance] Reuse superclass constructors in subclasses
[Python] How to make a class iterable
Playing card class in Python (with comparison)
Python basic operation 3rd: Object-oriented and class
[Python] Difference between class method and static method
Comparison of class inheritance and constructor description
How to use __slots__ in Python class
Generate a class from a string in Python
Python and NumPy numeric type inheritance relationship
Talking about Python class attributes and metaclasses