[Tutor] Concept related to python classes
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Sep 7 19:19:11 EDT 2020
On 07/09/2020 21:52, David Rock wrote:
>> There is also the suggestion I have heard that a class with two methods,
>> one being __init__, and one computing a result is often better as just a
>> function (unless you need the object as a scratch pad for some reason)
>
> That may be, but doing it as a class allows for easy expansion.
The YAGNI principle applies. The time to create a class is when
you know you need one. Otherwise you are just adding extra
layers of complexity. (Both conceptually and in the actual
executable code - extra work for the interpreter to look up
the class on top of calling the function.)
Classes are there to define objects, if there isn't an
object there shouldn't need to be a class. Its a classic
example of premature optimisation which actually gets
in the way for the initial case.
If you just need a bunch of functions that's what modules
are for.
> when calculating the perimeter becomes necessary? If it's a class, the
> framework is already there.
Of course, if you know that you will definitely be adding
more methods later then creating classes incrementally is
fine because you know it will have more than one method,
even if you start with only one. The aphorism is referring
to the many classes that exist with one and only one method
plus init()
--
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