0

Python 3.x 中从 python 的对象继承是否有必要或有用?并且Python类继承对象明确,在Python 3中定义类时不再需要继承自。object

作为对此的推论,任何一个链接问题都没有直接解决:在编写新的 Python 3 代码时,我是否应该更喜欢其中一种风格?为了更清晰的类定义而放弃object基类,还是为了(可能)使未来的 Python 2 移植更容易而保留它?

4

2 回答 2

2

编程正在将抽象概念转换为形式形式,然后用于生成可执行文件。没有必要使过程比必要的复杂。这些机器是为了帮助我们而创造的。它不适用于相反的方向。

从某种意义上说,Python 3一种新语言。那么问题应该是:“我应该强迫自己使用新语言,让它看起来像旧工具/程序员习惯的样子吗?”

又到了一一读Python之禅的时候了:

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
于 2013-12-27T08:01:19.077 回答
1

在 3.x 中定义类时,pylint 似乎仍然警告不要使用对象,所以我也添加了它,即使对于 3.x-only 代码也是如此。我写了很多在 2.x 和 3.x 上运行的 Python;你想要它很简单。

但是你为双方都提出了合理的论据,真的。你怎么

于 2013-12-27T06:11:59.263 回答