Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A global or module-level variable?

Reply
Thread Tools

A global or module-level variable?

 
 
Bret
Guest
Posts: n/a
 
      01-22-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:

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
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      01-22-2008
Bret <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Bret
Guest
Posts: n/a
 
      01-23-2008
On Jan 22, 1:00 pm, Paul Rubin <http://(E-Mail Removed)> wrote:

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


Is there a better way? A more Pythonic way?
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      01-23-2008
En Wed, 23 Jan 2008 11:58:05 -0200, Bret <(E-Mail Removed)> escribió:

> On Jan 22, 1:00 pm, Paul Rubin <http://(E-Mail Removed)> 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

 
Reply With Quote
 
Bret
Guest
Posts: n/a
 
      01-24-2008
On Jan 23, 2:27 pm, "Gabriel Genellina" <(E-Mail Removed)>
wrote:
> En Wed, 23 Jan 2008 11:58:05 -0200, Bret <(E-Mail Removed)> escribió:
>
> > On Jan 22, 1:00 pm, Paul Rubin <http://(E-Mail Removed)> 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!
 
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
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Could not load type 'Global.yaddayadda'. - Global.asax.cs Daniel Bass ASP .Net 2 12-31-2003 03:57 PM
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM
Global Error handling in Applicatio_Error() of Global.asax VSK ASP .Net 1 07-29-2003 03:12 AM



Advertisments