[Tutor] FUNCTIONS vs. CLASSES (early beginner questions)

Alan Gauld alan.gauld at yahoo.co.uk
Tue Mar 28 14:04:14 EDT 2017


On 28/03/17 17:45, Rafael Knuth wrote:
> Question: When should I use functions?
> When should I use classes?

Thee is no definitive answer but here are some
guidelines:

1) Many instances -> class
2) Many methods sharing the same data -> class
3) An algorithm that has no side effects -> a function

Very often we start with a function then realize it
would be better in a class.

In general classes need more code but provide more
maintainable and reusable solutions. so on a bigger
project classes are likely to be a better option.

Pythons module/package mechanism is quite powerful
so often we don't need classes where other
languages would use 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