当Is it necessary or useful to inherit from python's object in Python 3.x?和Python class inherits object明确时,在Python 3中定义类时,继承object
不再需要 。
作为一个必然结果,这个问题没有被任何一个链接问题直接解决:在编写新的Python 3代码时,我是否更喜欢使用另一种风格?为了更清晰的类定义而放弃object
基类是不是更好,还是留下它以便(可能)使Python 2的未来端口更容易?
答案 0 :(得分: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!
答案 1 :(得分:1)
在3.x中定义一个类时,pylint似乎仍然警告不使用对象,所以我也添加它,即使对于仅限3.x的代码也是如此。我写了很多在2.x和3.x上运行的Python;在那里你很想要它。
但是你确实为双方提出了合理的论据。 你认为什么?