question about class, functions and scope

N

nephish

lo there all,

i have an app that runs three classes as threads in the same program.
some of them need to be able to share some functions. Can i set a
function before i define a class and have the class use it ? Kinda like
this.

def some_function(var1, var2):
do something with var1, var2
return result

class do_something1(threading.Thread):
def __init__(var):
do something
def run():
var1 = 3
var2 = 4
result = some_function(var1,var2)

is this legal ? is it pythonic?
i ask because i plan to do a big re-write soon, and want to get rid of
some repetition

thanks
 
N

nephish

nephish said:
cool enough, and thanks for the quick reply.

shawn

one more question.
the functions defined above the classes that the could be called from
within the classes, they do not need a 'self' declaration because they
are not part of a class, right?

sk
 
B

bearophileHUGS

nephish:
one more question.
the functions defined above the classes that the could be called from
within the classes, they do not need a 'self' declaration because they
are not part of a class, right?

Class methods generally require the self as first parameter, functions
don't need the self. So your original code was wrong (sorry, I haven't
seen that before).

You can also inject functions as methods inside a class, and in such
case your function usually needs a self parameter too.

Bye,
bearophile
 
N

nephish

nephish:

Class methods generally require the self as first parameter, functions
don't need the self. So your original code was wrong (sorry, I haven't
seen that before).

You can also inject functions as methods inside a class, and in such
case your function usually needs a self parameter too.

Bye,
bearophile

ok, thanks much, thats all i needed to know.
shawn
 
G

Gabriel G

i have an app that runs three classes as threads in the same program.
some of them need to be able to share some functions. Can i set a
function before i define a class and have the class use it ? Kinda like
this.

def some_function(var1, var2):
do something with var1, var2
return result

It's ok - but beware of concurrency problems. By example, two threads
trying to update the same thing. (Doesn't happen in your small example).



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
G

Gabriel Genellina

would it be better to move all the functions i want to share into
some class and have the class threads refer to them that way ? i
mean, just to keep things seperate, but resuseable ?

That really depends on your design. Python does not enforce that
*all* code be in class methods; using functions alone -as others have
pointed out- is fine.



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 

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
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top