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

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
黄巍
字节跳动首席吃点心员
黄巍
Includes Chapters for Python Crash Course session.

python-crash-course Includes Chapters for Python Crash Course session. What will you learn: Python Essentials Creating Server Writing REST API Writing

Vineet Rao 3 Feb 17, 2021
Get a list of the top-10 rejected libraries in your WhiteSource inventory

WhiteSource Top 10 Rejected Libraries Generate a spreadsheet listing the 10 most common libraries in your WhiteSource inventory that were rejected by

WhiteSource-PS-tools 10 Mar 23, 2022
Convert Beat Saber maps to Tesla light shows!

Tesla x Beat Saber - Light Show Converter Convert Beat Saber maps to Tesla light shows! This project requires FFMPEG and all packages from requirement

HLVM 20 Dec 21, 2022
In this project, we are going to display the battery notification and the time left for the battery to drain out using the battery capacity value.

In this project, we are going to display the battery notification and the time left for the battery to drain out using the battery capacity value.

Ritoban Biswas 1 Dec 20, 2021
Aero is an open source airplane intelligence tool. Aero supports more than 13,000 airlines and 250 countries. Any flight worldwide at your fingertips.

Aero Aero supports more than 13,000 airlines and 250 countries. Any flight worldwide at your fingertips. Features Main : Flight lookup Aircraft lookup

Vickey 비키 4 Oct 27, 2021
Material de apoio da oficina de SAST apresentada pelo CAIS no Webinar de 28/05/21.

CAIS-CAIS Conjunto de Aplicações Intencionamente Sem-Vergonha do CAIS Material didático do Webinar "EP1. Oficina - Práticas de análise estática de cód

Fausto Filho 14 Jul 25, 2022
Hydralit package is a wrapping and template project to combine multiple independant Streamlit applications into a multi-page application.

Hydralit The Hydralit package is a wrapping and template project to combine multiple independant (or somewhat dependant) Streamlit applications into a

Jackson Storm 108 Jan 08, 2023
Webcash is an experimental e-cash (electronic cash)

Webcash Webcash is an experimental new electronic cash ("e-cash") that enables decentralized and instant payments to anyone, anywhere in the world. Us

Mark Friedenbach 0 Feb 26, 2022
A tool to help plan vacations with friends and family

Vacationer In Development A tool to help plan vacations with friends and family Deployment Requirements: NPM Docker Docker-Compose Deployment Instruct

JK 2 Oct 05, 2021
Linux Backlight Manager

Is a program to manage your laptop keyboard backlights in linux. Tested on Tuxedo / Clevo / Monste models. Must be tested on other devices

Arshia Ihammi 4 Jan 14, 2022
A Lego Mindstorm robot for dealing out cards based on a birds-eye view of a poker table and given ArUco fiducial tags.

A Lego Mindstorm robot for dealing out cards based on a birds-eye view of a poker table and given ArUco fiducial tags.

4 Dec 06, 2021
HOWTO: Downgrade from nYNAB to YNAB4

HOWTO: Downgrade from nYNAB to YNAB4 This page explains how to move from nYNAB to YNAB4 while retaining as much information as possible. See Appendix

Tobias Kunze 10 Dec 29, 2022
Different steganography methods with examples and my own small image database

literally-the-most-useless-project [Different steganography methods with examples and my own small image database] This project currently contains thr

Kamyishka 1 Dec 09, 2022
A TODO-list tool written in Python

PyTD A TODO-list tool written in Python. Its goal is to provide a stable posibility to get a good view over all your TODOs motivate you to actually fi

1 Feb 12, 2022
Eros is an expiremental programming language built using simple Python code.

Eros is an expiremental programming language built using simple Python code. Featuring an easy syntax and unique features like type slicing, the language remains an expirement that grows in down time

zxro 2 Nov 21, 2021
Shopping-card - Shopping Card Project With Python

Shopping Card Project this application was built to handle problems with saving

moein98 1 May 06, 2022
Mdisk - 🚧 On Construction 🚧

Mdisk Install For Package pip install mdisk pip install git+https://github.com/HeimanPictures/Mdisk.git Usage You can use this as python module or via

AkKiL 6 Aug 08, 2022
Experiments with Tox plugin system

The project is an attempt to add to the tox some missing out of the box functionality. Basically it is just an extension for the tool that will be loa

Volodymyr Vitvitskyi 30 Nov 26, 2022
Bazel rules to install Python dependencies with Poetry

rules_python_poetry Bazel rules to install Python dependencies from a Poetry project. Works with native Python rules for Bazel. Getting started Add th

Martin Liu 7 Dec 15, 2021
ELF file deserializer and serializer library

elfo ELF file deserializer and serializer library. import elfo elf = elfo.ELF.from_path('main') elf ELF( header=ELFHeader( e_ident=e

Filipe Laíns 3 Aug 23, 2021