[Tutor] Need to discuss about a class that can be used to print number of vowels, consonants, uppercase and lowercase letters and spaces in a string

Alan Gauld alan.gauld at yahoo.co.uk
Thu Aug 19 20:23:06 EDT 2021


On 19/08/2021 22:44, dn via Tutor wrote:

> +1, keep things 'together' and keep usage(s) 'local' and 'close'.
> 
> Isn't there a convention/habit (or perhaps has come-across from another
> language where it is a requirement) that all data-attributes should
> either be defined in the class or __init__()?

This is the dichotomy with this issue.

Normally I'd advocate initializing in the init().
But in this case the variables need to be reinitialized every time
compute() is called so you might as well do it once there and call
compute() from init().

> However, why not combine both suggestions? Conflate __init__() and
> compute_stats() - thus the consideration 'goes away'!


If you mean put the compute() code into init that raises the problem of
not being able to recompute the stats if the string changes.

OTOH strings are immutable in python so for consistency perhaps
we should require a new StringStats instance? But if we do that
we are even more strongly arguing that Stringstats is really
a function rather than a true class...

> (at which time, the class vs function decision will come into stark
> focus...)

Just so... If the class only has an init() and a __str__()
is it really a class?

-- 
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