Function within class and in modules

T

TheSaint

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.
 
R

Roy Smith

TheSaint said:
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.
 
Z

Zach Dziura

Hello
sorry, I'm bit curious to understand what could be the difference to packup
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!)
 
T

TheSaint

Zach said:
Just repeat this to yourself: Python ISN'T Java

I never had to do anything in Java. But mostly something in Sumatra :D
I'm getting the point that I'll need class very seldom.
Only to understand some more the use of self, whether I'll use a class.
 
A

Andrew Berg

Just repeat this to yourself: Python ISN'T Java.
class MainClass:
def public static void main(*args):
print('Am I doin' it right?')

:p

Or something like that. I've forgotten almost everything I learned about
Java.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top