[Tutor] Python scripts: to use class or not — what is the best practice?

Mats Wichmann mats at wichmann.us
Mon Mar 16 10:13:14 EDT 2020


On 3/15/20 8:07 PM, Igwe Kalu wrote:
> Hi Community,
> 
> I need your feedback on a possibly simple idea.
> 
> Python scripts: to use class or not — what is the best practice?<https://softwareengineering.stackexchange.com/questions/406585/python-scripts-to-use-class-or-not-what-is-the-best-practice>
> 
> The details of my question are posted on https://softwareengineering.stackexchange.com/questions/406585/python-scripts-to-use-class-or-not-what-is-the-best-practice
> 
> I would greatly appreciate your inputs there, thank you.

well, you'll have seen that Stackexchange doesn't like questions that
require an opinion, so like the others, I won't reply over there.

And that's really what you've done - you've veered into the opinion space.

Python is indeed very object oriented, in that everything you operate on
is an object.  It is *not* a language that forces you to wrap everything
you do in a class definition, if Guido thought that was
best-practice-for-all-uses, he presumably would have written the
language that way. So maybe take a hint from that?

Software design is complicated.  You want to build a program that is
practical for as many uses as possible, but don't want to overdesign it
to the point where you waste time on things that it won't be used for.
You have to balance building a design with lots of features, easily
expandable, with a consistent API with the need to get something working
which maybe won't actually get used widely enough to need all of that.
It's actually quite fine also to build something simply to throw away -
fitness for purpose.  Only you know what your requirements are for a
given project.

A class definition in Python is a new datatype. Do you need a new
datatype?  These normally abstract/encapsulate something... consider a
Python list type - it bundles some data storage together with functions
to act on it, like append(), extend(), sort(), etc.  That makes sense,
you can think of a "list" as a particular Thing. The example you
psuedo-coded doesn't seem to encapsulate any particularly interesting
behavior that would qualify it as being a datatype - but I recognize
that it was only an example, and some real cases may be different.





More information about the Tutor mailing list