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

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
黄巍
字节跳动首席吃点心员
黄巍
For radiometrically calibrating and PSF deconvolving IRIS data

irispreppy For radiometrically calibrating and PSF deconvolving IRIS data. I dislike how I need to own proprietary software (IDL) just to simply prepa

Aaron W. Peat 4 Nov 01, 2022
tool to automate exploitation of android degubg bridge vulnerability

DISCLAIMER DISCLAIMER: ANY MALICIOUS USE OF THE CONTENTS FROM THIS ARTICLE WILL NOT HOLD THE AUTHOR RESPONSIBLE HE CONTENTS ARE SOLELY FOR EDUCATIONAL

6 Feb 12, 2022
Albert launcher extension for rolling dice.

dice-roll-albert-ext Extension for rolling dice in Albert launcher Installation Locate the modules directory in the Python extension data directory. T

Jonah Lawrence 1 Nov 18, 2021
Project Guide for ASAM OpenX standards

ASAM Project Guide Important This guide is a work in progress and subject to change! Hosted version available at: ASAM Project Guide (Link) Includes:

ASAM e.V. 2 Mar 17, 2022
Voldemort's Python import helper

importmagician Voldemort's Python import helper pip install importmagician Import from uninstalled Python directories Say you have a directory (relat

Zhengyang Feng 4 Mar 09, 2022
Safe temperature monitor for baby's room. Made for Raspberry Pi Pico.

Baby Safe Temperature Monitor This project is meant to build a temperature safety monitor for a baby or small child's room. Studies have shown the ris

Jeff Geerling 72 Oct 09, 2022
Awesome Casino is simple offline casino made on python.

Awesome-Casino Awesome Casino is simple offline casino made on python. I found bug, what can i do? If you find any bug or want to suggest any idea, al

Herman 1 Feb 04, 2022
Null safe support for Python

Null Safe Python Null safe support for Python. Installation pip install nullsafe Quick Start Dummy Class class Dummy: pass Normal Python code: o =

Paaksing 13 Nov 17, 2022
VCC-Generator is a python script that generate VCC for testing purposes only

VCC-Generator is a python script that generate VCC for testing purposes only

Spider Anongreyhat 10 Oct 23, 2022
March-madness - March Madness results 1985-2021

march-madness Results for all 2,268 NCAA Division I Men's Basketball Tournament games since the modern format was introduced in 1985. Includes years,

Darik Harter 2 Feb 26, 2022
An open-source Python project series where beginners can contribute and practice coding.

Python Mini Projects A collection of easy Python small projects to help you improve your programming skills. Table Of Contents Aim Of The Project Cont

Leah Nguyen 491 Jan 04, 2023
Taxonomy addition for complete trees

TACT: Taxonomic Addition for Complete Trees TACT is a Python app for stochastic polytomy resolution. It uses birth-death-sampling estimators across an

Jonathan Chang 3 Jun 07, 2022
A toolkit for developing and deploying serverless Python code in AWS Lambda.

Python-lambda is a toolset for developing and deploying serverless Python code in AWS Lambda. A call for contributors With python-lambda and pytube bo

Nick Ficano 1.4k Jan 03, 2023
A wrapper around the python Tkinter library for customizable and modern ui-elements in Tkinter

CustomTkinter With CustomTkinter you can create modern looking user interfaces in python with tkinter. CustomTkinter is a tkinter extension which prov

4.9k Jan 02, 2023
Files relating to polymtl university

This is a tool I developed quickly, which allows users to visualize class availability by day of the week for a given program at polymtl. The schedule

PN 3 Mar 15, 2022
Rotating cube with hand

I am still working on this project :)) To-Do(Present): = It needs an algorithm to fine tune my hand's coordinates for rotation of our cube (initial o

Jay Desale 2 Dec 26, 2021
Android Blobs Organizer

Android Blobs Organizer

Sebastiano Barezzi 96 Jan 02, 2023
Org agenda in the console

This Python script reads an org agenda file (i.e. a regular org file with some active dates) and displays an interactive and colored year calendar with detailed information for each day when the mous

Nicolas P. Rougier 113 Jan 03, 2023
Make your Discord Account Online 24/7!

Online-Forever Make your Discord Account Online 24/7! A Code written in Python that helps you to keep your account 24/7. The main.py is the main file.

SealedSaucer 0 Mar 16, 2022
Module for working with the site dnevnik.ru with python

dnevnikru Module for working with the site dnevnik.ru with python Dnevnik object accepts login and password from the dnevnik.ru account Methods: homew

Aleksandr 21 Nov 21, 2022