Question about scope

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Oct 24 00:03:14 CEST 2008


On Thu, 23 Oct 2008 11:38:35 -0400, Pat wrote:

> I have a Globals class.

Well, that's your first mistake. Using global variables in a class is no 
better than using bare global variables. They're still global, and that's 
a problem:

http://weblogs.asp.net/wallen/archive/2003/05/08/6750.aspx

 
> In it, I have a variable defined something like this:
> 
> remote_device_enabled = bool
> 
> In one module, I assign True/False to Globals.remote_device_enabled.
> Once set, this value never changes.
> 
> In another module, at the top after the imports statements, I tried
> this:
> 
> from Globals import *

That can't work if Globals is a class. I take it you meant that Globals 
is a module. That's still got all the disadvantages of global variables.


 
> RDE = Globals.remote_device_enabled
> 
> This way, I thought that I could just use 'if RDE:'
> 
> Within the functions, however, I get a different value.  What am I
> misunderstanding?

Everything?

Perhaps it's time to go back to basics and work through the tutorial.

 
> I tried this at the top of the module (but it didn't word):
> 
> global RDE
> RDE =  Globals.remote_device_enabled

At the top of a module, the "global" keyword is a no-op, because 
everything at the top of a module is already global.


-- 
Steven



More information about the Python-list mailing list