Function within class and in modules

Roy Smith roy at panix.com
Wed Jun 15 08:18:59 EDT 2011


In article <ita6nj$4j4$1 at speranza.aioe.org>,
 TheSaint <nobody at nowhere.net.no> wrote:

> Hello
> sorry, I'm bit curious to understand what could be the difference to pack up 
> a class for some number of functions in it and a simple module which I just 
> import and use the similar functions?

If all you have is a bunch of functions, just sticking them in a module 
is fine.  The reason you would want to package them up as a class would 
be if there's some state that needs to be saved.

Don't think of a class as a collection of methods, think of it as a hunk 
of data, and some methods which operate on that data.  Looking at it 
another way, if you write a class and discover that none of the methods 
ever make any use of self, then what you probably really wanted to do 
was create a module to hold all those methods as top-level functions.



More information about the Python-list mailing list