Connecting Tech Pros Worldwide Forums | Help | Site Map

question about class, functions and scope

nephish
Guest
 
Posts: n/a
#1: Aug 26 '06
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


bearophileHUGS@lycos.com
Guest
 
Posts: n/a
#2: Aug 26 '06

re: question about class, functions and scope


nephish:
Quote:
is this legal ? is it pythonic?
It's legan and pythonic. Functions are here for a purpose.

Bye,
bearophile

nephish
Guest
 
Posts: n/a
#3: Aug 26 '06

re: question about class, functions and scope



bearophileHUGS@lycos.com wrote:
Quote:
nephish:
Quote:
is this legal ? is it pythonic?
>
It's legan and pythonic. Functions are here for a purpose.
>
Bye,
bearophile
cool enough, and thanks for the quick reply.

shawn

nephish
Guest
 
Posts: n/a
#4: Aug 26 '06

re: question about class, functions and scope



nephish wrote:
Quote:
bearophileHUGS@lycos.com wrote:
Quote:
nephish:
Quote:
is this legal ? is it pythonic?
It's legan and pythonic. Functions are here for a purpose.

Bye,
bearophile
>
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

bearophileHUGS@lycos.com
Guest
 
Posts: n/a
#5: Aug 26 '06

re: question about class, functions and scope


nephish:
Quote:
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

nephish
Guest
 
Posts: n/a
#6: Aug 26 '06

re: question about class, functions and scope



bearophileHUGS@lycos.com wrote:
Quote:
nephish:
Quote:
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
ok, thanks much, thats all i needed to know.
shawn

Gabriel G
Guest
 
Posts: n/a
#7: Aug 28 '06

re: question about class, functions and scope


At Saturday 26/8/2006 06:13, nephish wrote:
Quote:
>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

Gabriel Genellina
Guest
 
Posts: n/a
#8: Aug 28 '06

re: question about class, functions and scope


At Monday 28/8/2006 14:55, shawn bright wrote:
Quote:
>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

Closed Thread