[Tutor] Python scripts: to use class or not — what is the best practice?
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Mar 16 06:59:24 EDT 2020
On 16/03/2020 02:07, Igwe Kalu wrote:
> Python scripts: to use class or not — what is the best practice?
As with anything in programming, if a tool works use it.
Modules package up code for reuse beyond your current program.
If that is your aim use a module.
Classes bundle functions(methods) along with the data/state
needed by those functions. Classes allow for multiple
instances(objects) to be created. Classes generally model
some real or conceptual thing.
If that's what you are trying to do, use classes.
If the class can be reused elsewhere put your class
in a module.
If your program is object oriented then you will almost
certainly need to use classes. If your program is
primarily procedural (or functional) then you may find
classes convenient but equally you may not need them.
--
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