[Tutor] string not found in variable

Alan Gauld alan.gauld at yahoo.co.uk
Mon Mar 22 21:20:27 EDT 2021


On 22/03/2021 23:18, jark AJ wrote:

> 1) At what point you would have used classes in this case or what could be
> the use case that would decide us to choose classes over functions

A function is a single operation. It performs a task.
It takes in data and returns a result. It does not have
state(generator functions being a notable exception!)
So if you are thinking of a class to perform a single
task you probably want a function instead.

Classes represent objects. As such they have *attributes*
that manage the *internal state*. They also have a set
of *operations* by which the outside world communicates
with the objects (and indeed by which the *objects can
communicate* with each other).

So if, in your solution, you have some state data that
needs to have  various things done to it, especially if
those things are done over time, then a class is appropriate.

Also, if you need several instances of something
represented by a common group of data attributes
then you probably want a class (If there are no
operations then maybe you just need a dictionary plus
factory function instead)

To repeat the aphorism:
"If you have a class with a single method plus init()
then you really wanted a function."

There is a lot of programming overhead in creating a
class, and a lot of runtime overhead in instantiating
a class and calling its methods (compared to calling
a function). Don't create classes needlessly. Classes
do not necessarily imply better code. When they are
appropriate they are very useful, but only when
appropriate.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list