question about class, functions and scope

Discussion in 'Python' started by nephish, Aug 26, 2006.

  1. nephish

    nephish Guest

    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
     
    nephish, Aug 26, 2006
    #1
    1. Advertising

  2. nephish

    Guest

    nephish:
    > is this legal ? is it pythonic?


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

    Bye,
    bearophile
     
    , Aug 26, 2006
    #2
    1. Advertising

  3. nephish

    nephish Guest

    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
     
    nephish, Aug 26, 2006
    #3
  4. nephish

    nephish Guest

    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
     
    nephish, Aug 26, 2006
    #4
  5. nephish

    Guest

    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
     
    , Aug 26, 2006
    #5
  6. nephish

    nephish Guest

    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
     
    nephish, Aug 26, 2006
    #6
  7. nephish

    Gabriel G Guest

    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
     
    Gabriel G, Aug 28, 2006
    #7
  8. 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
     
    Gabriel Genellina, Aug 28, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    1,003
    Paul Opal
    Oct 11, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,662
    Victor Bazarov
    Jun 21, 2004
  3. Steven T. Hatton
    Replies:
    9
    Views:
    518
  4. Andrew Falanga
    Replies:
    2
    Views:
    216
    Andrew Falanga
    Nov 22, 2008
  5. Shriramana Sharma
    Replies:
    4
    Views:
    322
    Bart van Ingen Schenau
    May 5, 2013
Loading...

Share This Page