| Store | Cart

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

From: Alan Gauld via Tutor <tut...@python.org>
Tue, 28 Mar 2017 19:04:14 +0100
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


_______________________________________________
Tutor maillist  -  Tut...@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Recent Messages in this Thread
Rafael Knuth Mar 28, 2017 04:45 pm
Alan Gauld via Tutor Mar 28, 2017 06:04 pm
Messages in this thread