简体   繁体   English

从python 3.x中的父类继承

[英]Inheriting from a parent class in python 3.x

当您从python 3.x中的父类继承时,父类是否将其默认值“清空”,是否需要重新定义它们?

Software should try to be consistent. 软件应尽量保持一致。 So if you split a class into methods and fields, they should still behave similarly. 因此,如果将类划分为方法和字段,它们的行为仍应相似。 For methods, you expect to inherit them from your parent without any modifications unless you can see such a modification in the child. 对于方法,除非您在子级中看到这样的修改,否则您希望从其父级继承它们而无需进行任何修改。

For symmetry, fields behave the same: Unless you overwrite them in the child, you will get what the parent defines. 对于对称性,字段的行为相同:除非您在子级中覆盖它们,否则您将获得父级定义的内容。

From a more practical perspective, computers are always too slow. 从更实际的角度来看,计算机总是太慢。 So software tries to avoid unnecessary work. 因此,软件试图避免不必要的工作。 When you inherit from a parent, as little as possible is changed. 当您从父母那里继承时,尽可能少的更改。

This article gives a lot of insight how inheritance works in Python. 本文提供了很多关于继承如何在Python中工作的见解。

Any methods not overridden in the child are looked for in the parents. 在父母中寻找任何未在孩子中超越的方法。 This includes the initializer, __init__() . 这包括初始化器__init__() If you do not define this in the child then the parent's method is called, initializing the object as it would an instance of the parent. 如果未在子级中定义此对象,则将调用父级的方法,并像父级实例一样初始化对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM