[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

A global or module-level variable?

bret.wortman@gmail.com

1/22/2008 7:53:00 PM

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
4 Answers

Paul Rubin

1/22/2008 8:01:00 PM

0

Bret <bret.wortman@gmail.com> 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.

bret.wortman@gmail.com

1/23/2008 1:58:00 PM

0

On Jan 22, 1:00 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:

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

Is there a better way? A more Pythonic way?

Gabriel Genellina

1/23/2008 9:28:00 PM

0

En Wed, 23 Jan 2008 11:58:05 -0200, Bret <bret.wortman@gmail.com> escribió:

> On Jan 22, 1:00 pm, Paul Rubin <http://phr...@NOSPAM.invalid> 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

bret.wortman@gmail.com

1/24/2008 3:50:00 PM

0

On Jan 23, 2:27 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Wed, 23 Jan 2008 11:58:05 -0200, Bret <bret.wort...@gmail.com> escribió:
>
> > On Jan 22, 1:00 pm, Paul Rubin <http://phr...@NOSPAM.invalid> 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!