A global or module-level variable?

Discussion in 'Python' started by Bret, Jan 22, 2008.

  1. Bret

    Bret Guest

    This has to be easier than I'm making it....

    I've got a module, remote.py, which contains a number of classes, all
    of whom open a port for communication. I'd like to have a way to
    coordinate these port numbers akin to this:

    So I have this in the __init__.py file for a package called cstore:

    nextport=42000

    def getNextPort():
    nextport += 1
    return nextport

    :
    Then, in the class where I wish to use this (in cstore.remote.py):
    :


    class Spam():

    def __init__(self, **kwargs):
    self._port = cstore.getNextPort()

    I can't seem to make this work, though. As given here, I get an
    "UnboundLocalError:local variable 'nextport' referenced before
    assignment". When I try prefixing the names inside __init__.py with
    "cstore.", I get an error that the global name "cstore" is not
    defined.

    I've been looking at this long enough that my eyes are blurring. Any
    ideas?

    BTW, the driving force here is that I'm going to need to wrap this in
    some thread synchronization. For now, though, I'm just trying to get
    the basics working.

    Thanks!


    Bret
     
    Bret, Jan 22, 2008
    #1
    1. Advertising

  2. Bret

    Paul Rubin Guest

    Bret <> writes:
    > nextport=42000
    >
    > def getNextPort():
    > nextport += 1
    > return nextport


    If you have to do it that way, use:

    def getNextPort():
    global nextport
    nextport += 1
    return nextport

    the global declaration stops the compiler from treating nextport as
    local and then trapping the increment as to an uninitialized variable.
     
    Paul Rubin, Jan 22, 2008
    #2
    1. Advertising

  3. Bret

    Bret Guest

    On Jan 22, 1:00 pm, Paul Rubin <http://> wrote:

    > If you have to do it that way, use:


    Is there a better way? A more Pythonic way?
     
    Bret, Jan 23, 2008
    #3
  4. En Wed, 23 Jan 2008 11:58:05 -0200, Bret <> escribió:

    > On Jan 22, 1:00 pm, Paul Rubin <http://> wrote:
    >
    >> If you have to do it that way, use:

    >
    > Is there a better way? A more Pythonic way?


    It's simple, clear and works fine, why make it more complicated?
    Unless you have additional requirements, like a multithreaded program.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 23, 2008
    #4
  5. Bret

    Bret Guest

    On Jan 23, 2:27 pm, "Gabriel Genellina" <>
    wrote:
    > En Wed, 23 Jan 2008 11:58:05 -0200, Bret <> escribió:
    >
    > > On Jan 22, 1:00 pm, Paul Rubin <http://> wrote:

    >
    > >> If you have to do it that way, use:

    >
    > > Is there a better way? A more Pythonic way?

    >
    > It's simple, clear and works fine, why make it more complicated?
    > Unless you have additional requirements, like a multithreaded program.
    >
    > --
    > Gabriel Genellina


    Ultimately, it will be multithreaded -- and I had intended to wrap the
    access to the global member in a lock to ensure only one thread could
    get a value at any point. It might also become multiprocess (ugh!)
    and so might need to live in its own SocketServer some day. But for
    now, I agree. Simple is good. I just wanted to be sure I wasn't
    oversimplifying, you know?

    Thanks!
     
    Bret, Jan 24, 2008
    #5
    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. Mudcat
    Replies:
    3
    Views:
    971
    Steven D'Aprano
    Jan 14, 2006
  2. DesCF
    Replies:
    2
    Views:
    405
    DesCF
    Nov 11, 2006
  3. Replies:
    1
    Views:
    338
    Diez B. Roggisch
    Oct 29, 2006
  4. pabbu
    Replies:
    8
    Views:
    772
    Marc Boyer
    Nov 7, 2005
  5. Sam Kong
    Replies:
    2
    Views:
    191
    Joel VanderWerf
    May 25, 2005
Loading...

Share This Page