[Tutor] Python 3 only: Better to use implicit or explicit "object" in class statements?

Richard Damon Richard at Damon-Family.org
Sun Jun 7 15:35:39 EDT 2020


On 6/7/20 1:51 PM, 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.
>
As you note, the (object) was needed in Python2 to make it a "New Style"
class, while in Python 3 that is the only style class that exists.

In Python3, it is purely optional, and its presence is merely stylistic.
The big question comes, are you working on a project or with a group of
programmers used to dealing with Python2, so have the habit of the
explicit reference to object, or is the project brand new with fresh
developers, for whom the (object) would be just noise?

I suspect that most new projects will actually fall somewhat in the
middle, not needing to be compatible with Python 2 (let it rest in
peace) but with some developers still with older habits, and a decision
needing to be made as to which style is more readable TO THAT TEAM.

There of course are also 'zealots' who are frustrated by the extremely
slow demise of Python 2 and want to purge its memory from the style and
therefore want to insist on dropping the now unneeded (object) from the
definition, being afraid of Python 2 saying "I'm not dead yet", and
wanting to try to hammer the nails in the coffin to be rid of it.

-- 
Richard Damon



More information about the Tutor mailing list