[Tutor] Is there a preference of "class MyClass:" or "class MyClass(object):" for Py3?

Steven D'Aprano steve at pearwood.info
Sat Jan 16 00:47:42 EST 2016


On Fri, Jan 15, 2016 at 09:30:57PM -0600, boB Stepp wrote:

> Pythonic style/preference question:  For strictly Python 3 code, is
> there any preference for
> 
> class MyClass:
>     pass
> 
> versus the more explicit
> 
> class MyClass(object):
>     pass
> 
> ?

For *purely* Python 3 code, where your audience (readers, maintainers, 
developers etc) are all familiar with, and expect, Python 3 semantics, 
not really. Maybe if you are writing introspection code which explicitly 
works with the class MRO or base-classes, you might prefer to be 
explicit. Otherwise, do whatever you feel best. For quick and dirty 
throw-away code, feel free to leave out the base class.

If there's any chance that the code might be used in Python 2, or copied 
into a Python 2 module, or read by people expecting Python 2 semantics, 
then you ought to be explicit about the base class.


-- 
Steve


More information about the Tutor mailing list