[Tutor] Python 3 only: Better to use implicit or explicit "object" in class statements?
Mats Wichmann
mats at wichmann.us
Sun Jun 7 14:15:03 EDT 2020
On 6/7/20 11:51 AM, boB Stepp wrote:
> Now in "4.1 Classes" at
> https://dabeaz-course.github.io/practical-python/Notes/04_Classes_objects/01_Class.html.
>
>
> Basic question for Python 3 only development: Is it better style to use
> "class ClassName:" or "class ClassName(object):"? Beazley uses the former
> in this material (so far). Other readings online (no citations handy)
> prefer being explicit with the latter. I note that pylint prefers omitting
> "(object)", calling it "useless-object-inheritance".
>
> I realize that if I am trying to write code compatible with both Python 2
> and 3 that I need to explicitly inherit from "object".
>
> So, for Python 3 only development is there any consensus? Are there any
> real arguments for being "explicit" with inheriting from "object"? I have
> to say that I have not yet found the allusion to Zen to be explicit not
> convincing.
Consensus? Hah!
It seems kind of odd to need to supply something that is a guaranteed
language feature:
>>> class Spam:
... pass
...
>>> Spam.__mro__
(<class '__main__.Spam'>, <class 'object'>)
>>>
But then, there can't be any confusion if you're explicit, and if you're
writing code which might be considered for use in Python 2, then it's
easier to keep it.
I don't use (object), and recently submitted patches to a 20-year old
project that has recently become Python3-only to drop them, which the
maintainer accepted.
Here's my claim: listen to pylint, or someday you'll end up having to
defend why you're doing something pylint complains about. :) Tools
like Pylint and Black are pretty opinionated (although Black is so
opinionated that you can't override most of its choices, while Pylint is
almost infintely configurable), and sometimes it's just easier to go
with the flow.
More information about the Tutor
mailing list