【教程】莉沫酱教你学继承!?

Related tags

Miscellaneousinherit
Overview

【教程】莉沫酱教你学继承!

众所周知,类的继承就是说当一个类死亡的时候,它的子类会获得它拥有的资源。

根据类的继承法不同,各个子类能获得的资源也不同。

继承法的类型

在解释继承法之前,我们先定义三个类,一个父类A,和它的子类BC

它们都拥有xyz三个属性。

class A:
    x = 100
    y = 200
    z = 300
class B(A):
    x = y = z = 9
class C(A):
    x = y = z = 0

长子继承制

假如父类A采用了长子继承制,那么在继承中,所有资源都会被交给其最年长的子类,其他子类什么也得不到。

@长子继承制
class A:
    x = 100
    y = 200
    z = 300

在使用了长子继承制之后,我们杀掉父类,看看子类的资源有什么变化——

print(f'{A.x=}, {A.y=}, {A.z=}')
print(f'{B.x=}, {B.y=}, {B.z=}')
print(f'{C.x=}, {C.y=}, {C.z=}')
print('====继承====')
A.__del__()
print(f'{B.x=}, {B.y=}, {B.z=}')
print(f'{C.x=}, {C.y=}, {C.z=}')

可以看到,这个时候父类的所有属性都被第一个子类——也就是B——继承了。

A.x=100, A.y=200, A.z=300
B.x=9, B.y=9, B.z=9
C.x=0, C.y=0, C.z=0
====继承====
B.x=100, B.y=200, B.z=300
C.x=0, C.y=0, C.z=0

分割继承制

但是在软件开发的早期,我们无法import 长子继承制,因此我们得考虑一些其他的继承法,比如分割继承制

分割继承制中,父类的所有资源都会被划分给其子类。

@分割继承制
class A:
    x = 100
    y = 200
    z = 300

在继承之后,子类B获得了属性xz,而子类C获得了属性y

A.x=100, A.y=200, A.z=300
B.x=9, B.y=9, B.z=9
C.x=0, C.y=0, C.z=0
====继承====
B.x=100, B.y=9, B.z=300
C.x=0, C.y=200, C.z=0

斯堪的纳维亚选举制

斯堪的纳维亚选举制是北欧的维京程序员喜欢的继承法。

在这种制度下,程序中所有没有被回收的类都可以在该类的亲戚中提名一位继承人,最终由投票得分最高的类继承所有资源。

@斯堪的纳维亚选举制
class A:
    x = 100
    y = 200
    z = 300

在类A死亡时,所有的类都会在类B和类C中投票。尽管在这个例子里BC都是A的子类,实际上其他类也能投给A的兄弟等能DFS到的类。

通常,其他类会倾向于投给名字和自己长得比较像的类,一次典型的选举过程如下:

====继承====
_OrderedDictKeysView 投给 C
_OrderedDictItemsView 投给 C
_OrderedDictValuesView 投给 C
_Link 投给 C
Counter 投给 C
ChainMap 投给 C
UserDict 投给 C
UserList 投给 C
UserString 投给 C
partialmethod 投给 C
CacheInfo 投给 C
_HashedSeq 投给 B
singledispatchmethod 投给 C
cached_property 投给 B
AbstractContextManager 投给 C
AbstractAsyncContextManager 投给 C
ContextDecorator 投给 C

...

C: 142票
B: 133票
B.x=9, B.y=9, B.z=9
C.x=100, C.y=200, C.z=300

最终类C在选举中胜出,并继承了其父类A的所有属性。

使用方法

首先你需要1个3.6以上版本的Python,然后使用pip安装——

pip install git+https://github.com/RimoChan/inherit.git

然后你就可以在你的代码里使用继承法了,耶!

from 继承法 import 长子继承制, 分割继承制, 斯堪的纳维亚选举制

赞助

如果你觉得学习继承法对你的工作或学习有帮助(?),欢迎给作者介绍一些可爱的萝莉朋友。

就这样,大家88。

Owner
黄巍
字节跳动首席吃点心员
黄巍
Find virtual hosts (vhosts) from IP addresses and hostnames

Features Enumerate vhosts from a list of IP addresses and domain names. Virtual Hosts are enumerated using the following process: Supplied domains are

3 Jul 09, 2022
ASVspoof 2021 Baseline Systems

ASVspoof 2021 Baseline Systems Baseline systems are grouped by task: Speech Deepfake (DF) Logical Access (LA) Physical Access (PA) Please find more de

91 Dec 28, 2022
Purge your likes and wall comments from VKontakte. Set yourself free from your digital footprint.

vk_liberator Regain liberty in the cruel social media world. This program assists you with purging your metadata from Russian social network VKontakte

20 Jun 11, 2021
Software that extracts spreadsheets from various .pdf files to .csv

Extração de planilhas de diversos arquivos .pdf para .csv O código inteiro foi desenvolvido em Python. Foi utilizado o pacote "tabula" e a biblioteca

Marcos Silva 2 Jan 09, 2022
Tool to automate the enumeration of a website (CTF)

had4ctf Tool to automate the enumeration of a website (CTF) DISCLAIMER: THE TOOL HAS BEEN DEVELOPED SOLELY FOR EDUCATIONAL PURPOSE ,I WILL NOT BE LIAB

Had 2 Oct 24, 2021
A domonic-like wrapper around selectolax

A domonic-like wrapper around selectolax

byteface 3 Jun 23, 2022
Telegram bot for Urban Dictionary.

Urban Dictionary Bot @TheUrbanDictBot A star ⭐ from you means a lot to us! Telegram bot for Urban Dictionary. Usage Deploy to Heroku Tap on above butt

Stark Bots 17 Nov 24, 2022
Student Result Management System Project in tkinter created based on python, tkinter, and SQLITE3 Database

Student-Result-Management-System This Student Result Management System Project in tkinter created based on python, tkinter, and SQLITE3 Database. The

Ravi Chauhan 2 Aug 03, 2022
A modern python module including many useful features that make discord bot programming extremely easy.

discord-super-utils Documentation Secondary Documentation A modern python module including many useful features that make discord bot programming extr

106 Dec 19, 2022
Christmas tree on the desktop.

new-year-tree Christmas tree on the desktop. [Ссылка на статью habr]

Daniil Gorbenko 10 Dec 29, 2022
The bidirectional mapping library for Python.

bidict The bidirectional mapping library for Python. Status bidict: has been used for many years by several teams at Google, Venmo, CERN, Bank of Amer

Joshua Bronson 1.2k Dec 31, 2022
Files for QMC Workshop 2021

QMC Workshop 2021 This repository contains the presented slides and example files for the Quantum Monte Carlo (QMC) Workshop 5 October - 23 November,

QMCPACK 39 Nov 04, 2022
PyDy, short for Python Dynamics, is a tool kit written in the Python

PyDy, short for Python Dynamics, is a tool kit written in the Python programming language that utilizes an array of scientific programs to enable the study of multibody dynamics. The goal is to have

PyDy 307 Jan 01, 2023
Control your gtps with gtps-tools!

Note Please give credit to me! Do not try to sell this app, because this app is 100% open source! Do not try to reupload and rename the creator app! S

Jesen N 6 Feb 16, 2022
Flight Reservation App With Python

Flight Reservation App With Python

victor-h. 1 Nov 21, 2021
This is a Python script to detect rapid upwards price changes (pumps) in a cryptocurrency pairing

A python script to detect a rapid upwards price brekout (pump) in a cryptocurrency pairing, through pandas and Binance API.

3 May 25, 2022
Algorand Python API examples

Algorand-Py Algorand Python API examples This repo will hold example scripts to monitor activities on Algorand main net. You can: Monitor your assets

Karthik Dutt 2 Jan 23, 2022
Extended functionality for Namebase past their web UI

Namebase Extended Extended functionality for Namebase past their web UI.

RunDavidMC 12 Sep 02, 2022
Tips that improve your life in one way or another

Tips that improve your life in one way or another. This software downloads life tips from reddit.com/r/LifeProTips and tweet the most upvoted tips on Twitter.

Burak Tokman 2 Aug 04, 2022
A simple calculator made with tkinter.

Simple Calculator A simple calculator made with tkinter. Requirements None, only you need to have windows 😉 ...Enjoy! Installation Clone this reposit

Abhyush 2 Jan 11, 2022