A global or module-level variable?

Bret bret.wortman at gmail.com
Tue Jan 22 20:52:30 CET 2008

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:


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

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

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.



More information about the Python-list mailing list