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

DL Neil PyTutor at danceswithmice.info
Sun Jun 7 16:51:05 EDT 2020


On 8/06/20 7:35 AM, Richard Damon wrote:
> 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.


I enjoy being made to step-back and think...

I'm about as far from being a 'zealot' for lint-ing as one can get, but 
have a keen appreciation for the psychology of consistency (both amongst 
a team and 'within' an individual - without that, lies madness!)

These days I'm exclusively using Python3 and think that all 
active-clients and their project have been upgraded - but that from 
'common sense' rather than any agenda to 'kill' Python2.


Having come from those days (when men were men, knights were bold, ...) 
my 'muscle-memory' adds parentheses when defining ClassNames (ie without 
conscious thought).

I have learned not to add "object" between (he's 'slow', but usually he 
gets 'there'!).

However, there is still some vestige of recognition in my mind, that the 
parentheses 'make' it a class (wrong!) - or make it easier to recognise 
the code-line as-such (which assumes reading-mode cf asking one's IDE to 
locate/display the class definition).

 From the other perspective, a sub-class *must* use a parenthetical 
expression:

     class LinksRegister( collections.UserList ):

cf

     class LinksRegister():	# being consistent!?
     class LinksRegister:	# being precise!?

Hence the unadorned option, whilst 'the most correct', looks slightly 
odd (to me). Cue: gather around and sing "Tradition"*

Pycharm (in as much as it embodies PEPs, linters, and style) whilst 
typing a new class definition offers "':' expected". If empty 
parentheses are added, it then instructs "Remove redundant parentheses".

Does a touch-typist (a real man, a knight, ...) even notice these 
messages? Nah!
(Woe is me...)


* Fiddler on the Roof https://www.imdb.com/title/tt0067093/?ref_=fn_al_tt_1
-- 
Regards =dn


More information about the Tutor mailing list