Function within class and in modules

Zach Dziura zcdziura at gmail.com
Wed Jun 15 09:57:41 EDT 2011


> On Jun 15, 7:57 am, TheSaint <nob... 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?
> The only perspective that I think of is that class might instantiate a
> function several time. For my use I don't have multithread and mostly
> programs are sequencial.

I had a hard time with this at first when I started using Python. I
personally come from a background of using Java in an educational
environment, so I'm pretty familiar with it. Going from a "pure"
Object-Oriented language, where everything MUST be bundled into a
class, and an Object is less of "a bit of data" and more of "a data
structure that actually DOES something", was a little difficult.

Just repeat this to yourself: Python ISN'T Java. Repeat it until the
words start sounding funny to you. Then continue for another 10
minutes. It'll sink in.

Anyhow... In Python, classes aren't necessarily treated as "things
that do stuff" (though they can DEFINITELY act in that way!). Python
classes are made to hold data. If you have something that you need to
save for later, put it in a class and call it a day. If you only have
a bunch of functions that are meant to process something, just put
them into a module. You'll save yourself some time, you won't have to
instantiate a class in order to call the functions, and you'll be
happier overall. (I know was happy from being freed from the Pure OO
model that Java shoves down your throat!)



More information about the Python-list mailing list