Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > question about class, functions and scope

Reply
Thread Tools

question about class, functions and scope

 
 
nephish
Guest
Posts: n/a
 
      08-26-2006
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

 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      08-26-2006
nephish:
> is this legal ? is it pythonic?


It's legan and pythonic. Functions are here for a purpose.

Bye,
bearophile

 
Reply With Quote
 
 
 
 
nephish
Guest
Posts: n/a
 
      08-26-2006

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

 
Reply With Quote
 
nephish
Guest
Posts: n/a
 
      08-26-2006

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

 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      08-26-2006
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

 
Reply With Quote
 
nephish
Guest
Posts: n/a
 
      08-26-2006

wrote:
> 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


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

 
Reply With Quote
 
Gabriel G
Guest
Posts: n/a
 
      08-28-2006
At Saturday 26/8/2006 06:13, nephish wrote:

>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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-28-2006
At Monday 28/8/2006 14:55, shawn bright wrote:

>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57