Basic story of inheritance in Python (for beginners)

It's a basic story because my acquaintance was stumbling.

Introduction

For example, suppose you define the following classmethod in Python:

python


class Parent(object):
    name = "Parent"

    @classmethod
    def get_name(cls):
        return cls.name


class Child(Parent):
    name = "Child"

By the way, what is output when calling from Child at this time?

python


In : Child.get_name()
OUT: "Child"

A little commentary

Basically, the first argument defined in class binds the called instance or the class itself. Therefore, even if it is inherited, the class at the inherited destination will be entered. So, for example, it's OK if you don't make such a strange definition.

python


class Child(Parent):
    name = "Child"
    get_name = Parent.get_name

In this regard, the method basically has the context of "where it belongs", so in this case the method is executed according to the context of the referrer Parent, no matter what the class is.

Recommended Posts

Basic story of inheritance in Python (for beginners)
Basic Python grammar for beginners
[For beginners] Learn basic Python grammar for free in 5 hours!
Run unittests in Python (for beginners)
[Python] Minutes of study meeting for beginners (7/15)
Summary of various for statements in Python
The story of low learning costs for Python
Basic sorting in Python
python textbook for beginners
Image processing? The story of starting Python for
Try to calculate RPN in Python (for beginners)
Easy understanding of Python for & arrays (for super beginners)
The story of reading HSPICE data in Python
Basic knowledge of Python
OpenCV for Python beginners
[Introduction for beginners] Working with MySQL in Python
Data analysis in Python Summary of sources to look at first for beginners
Basic data frame operations written by beginners in a week of learning Python
Rock-paper-scissors poi in Python for beginners (answers and explanations)
[For beginners] How to use say command in python!
Summary of pre-processing practices for Python beginners (Pandas dataframe)
The story of FileNotFound in Python open () mode ='w'
Python Exercise for Beginners # 1 [Basic Data Types / If Statements]
Refactoring Learned in Python (Basic)
Learning flow for Python beginners
Search for strings in Python
Equivalence of objects in Python
Techniques for sorting in Python
Python3 environment construction (for beginners)
Python #function 2 for super beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Basic usage of Python f-string
Python #list for super beginners
Implementation of quicksort in Python
~ Tips for beginners to Python ③ ~
Basic summary of data manipulation in Python Pandas-Second half: Data aggregation
[For beginners] Basics of Python explained by Java Gold Part 2
Check the operation of Python for .NET in each environment
[Note] List of basic commands for building python / conda environment
■ Kaggle Practice for Beginners --Introduction of Python --by Google Colaboratory
A memo of writing a basic function in Python using recursion
[Python] The biggest weakness / disadvantage of Google Colaboratory [For beginners]
What beginners learned from the basics of variables in python
[For beginners] Basics of Python explained by Java Gold Part 1
[For beginners of competitive pros] Three input methods to remember when starting competitive programming in Python
The story of Python and the story of NaN
Check for external commands in python
Python Exercise for Beginners # 2 [for Statement / While Statement]
[Python of Hikari-] Chapter 09-03 Class (inheritance)
The story of participating in AtCoder
Scraping with Selenium in Python (Basic)
Python for super beginners Python # dictionary type 1 for super beginners
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
[Python] Basic knowledge used in AtCoder
Implementation of life game in Python
Python #index for super beginners, slices
<For beginners> python library <For machine learning>
The story of the "hole" in the file