Question about scope

L

Lave

Newbie too. I think you shoud qualify Global with the module name.

I have a Globals class.

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 *
from said:
RDE = Globals.remote_device_enabled
Here,
 
P

Pat

I have a Globals class.

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 *

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?

I tried this at the top of the module (but it didn't word):

global RDE
RDE = Globals.remote_device_enabled

Of course, within a function, the variable using the same two lines of
code assigns the correct value to RDE.

Thank you,

Total Newbie
 
B

Bruno Desthuilliers

Pat a écrit :
I have a Globals class.

Not sure it's such a great idea, but anyway... What's the use case for
this class ? There are perhaps better (or at least more idiomatic)
solutions...
In it, I have a variable defined something like this:

remote_device_enabled = bool

Could you show actual code ? It would really help. But it seems your
'Globals' class is mostly 1/ a singleton and 2/ used for application
wide settings. Is that right ?
In one module, I assign True/False to Globals.remote_device_enabled.

Directly to the class ?

Please, once again, provide real code. Well... not necessarily your
whole code, but at least minimal working code that reproduces the problem.
Once set, this value never changes.

In another module, at the top after the imports statements, I tried this:

from Globals import *

<ot>
The convention is to use lower case names for modules (and MixedCase
names for classes). This avoids confusion between synonym classes and
modules...
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?

Not enough informations, and my crystal ball is out for repair. Sorry.
Perhaps some actual code may help ?-)
I tried this at the top of the module (but it didn't word):

global RDE

Outside a function body, the 'global' statement is a no-op. In Python,
'global' really means 'module-level', so anything defined at the module
level is already as global as it can be.
RDE = Globals.remote_device_enabled

Of course, within a function, the variable using the same two lines of
code assigns the correct value to RDE.

Sorry Pat, but there's just not enough context for us to guess what's
wrong. It's easy enough to get it wrong with real code, so trying to
guess is just a waste of time.
 
S

Steven D'Aprano

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.
 
S

Steven D'Aprano

Stripping out the extra variables and definitions, this is all that
there is.
Whether or not this technique is *correct* programming is irrelevant.

Oh rly?

Well, sure, you can write bad code if you like, and make your actual job
much harder. No skin off our nose, except when you come along asking for
free debugging out of the goodness of our hearts.

I simply want to know why scoping doesn't work like I thought it would.


---> myGlobals.py file:

class myGlobals():
remote_device_enabled = bool

Creates a class myGlobals with a class attribute called
"remote_device_enabled" which is equal to the class bool.

Why not just initialise it to True here?

Why is it a class attribute instead of an instance attribute?

---> my initialize.py file:

from myGlobals import *

Creates a name called "myGlobals" which is local to this module. It is
bound to the same myGlobals class as defined by myGlobals.py module.

def initialize():
myGlobals.remote_device_enabled = True

Sets the class attribute remote_device_enabled to a useful value at last.

---> my main.py file:

import from myGlobals import *

Gives a Syntax error.

If you're not going to be bothered to test the code before you send it,
I'm not sure I can be bothered to debug it for you.

RDE = myGlobals.remote_device_enabled

Creates a local name RDE which takes its initial value from the class
attribute remote_device_enabled. This could be more easily written as:

RDE = bool

since initialize() hasn't been called yet.

def main():
if RDE: # this will not give me the correct value
process_device()

Obvious something else is happening in your real code, because following
the program logic you've shown (ignoring the SyntaxError), it should give
the correct value: RDE is bool. By a lucky accident, "if bool" evaluates
as True and process_device() will be called.

I suggest that your spaghetti code is far more complicated and you
haven't successfully teased out a minimal thread that demonstrates the
problem.
 
P

Pat

Bruno said:
Pat a écrit :

Not sure it's such a great idea, but anyway... What's the use case for
this class ? There are perhaps better (or at least more idiomatic)
solutions...


Could you show actual code ? It would really help. But it seems your
'Globals' class is mostly 1/ a singleton and 2/ used for application
wide settings. Is that right ?


Directly to the class ?

Please, once again, provide real code. Well... not necessarily your
whole code, but at least minimal working code that reproduces the problem.


<ot>
The convention is to use lower case names for modules (and MixedCase
names for classes). This avoids confusion between synonym classes and
modules...


Not enough informations, and my crystal ball is out for repair. Sorry.
Perhaps some actual code may help ?-)


Outside a function body, the 'global' statement is a no-op. In Python,
'global' really means 'module-level', so anything defined at the module
level is already as global as it can be.


Sorry Pat, but there's just not enough context for us to guess what's
wrong. It's easy enough to get it wrong with real code, so trying to
guess is just a waste of time.


Stripping out the extra variables and definitions, this is all that
there is.
Whether or not this technique is *correct* programming is irrelevant. I
simply want to know why scoping doesn't work like I thought it would.


---> myGlobals.py file:

class myGlobals():
remote_device_enabled = bool

---> my initialize.py file:

from myGlobals import *
def initialize():
myGlobals.remote_device_enabled = True

---> my main.py file:

import from myGlobals import *
RDE = myGlobals.remote_device_enabled

def main():
if RDE: # this will not give me the correct value
process_device()
 
B

Bruno Desthuilliers

Pat a écrit :
(snip)
Stripping out the extra variables and definitions, this is all that
there is.
Whether or not this technique is *correct* programming is irrelevant.

It's obviously relevant. If it was correct, it would work, and you
wouldn't be asking here !-)
I
simply want to know why scoping doesn't work like I thought it would.


---> myGlobals.py file:

class myGlobals():
remote_device_enabled = bool

<irrelevant>
You're using the class as a bare namespace. FWIW, you could as well use
the module itself - same effect, simplest code.
---> my initialize.py file:

from myGlobals import *
def initialize():
myGlobals.remote_device_enabled = True
---> my main.py file:

import from myGlobals import *

I assume the first "import" is a typo. But this sure means you didn't
run that code.
RDE = myGlobals.remote_device_enabled

def main():
if RDE: # this will not give me the correct value

For which definition of "correct value" ? You didn't import nor execute
initialize() so far, so at this stage RDE is bound to the bool type
object. FWIW, note that calling initialize *after* the assignement to
RDE won't change the fact that RDE will be still bound to the the bool
type object.

<irrelevant>
You may want to have a look at how other Python application manage
application-wide settings.
</irrelevant>
 
B

Bruno Desthuilliers

Lawrence D'Oliveiro a écrit :
Singleton class.

Possibly, yes (and I believe it is the case, but...). Or the OP doesnt
have a good enough understanding of Python's object model, in which case
Steven's question makes sense.
 
P

Pat

Steven said:
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

It depends upon the situation.

In my program, I have one routine that loads a bunch of files and
initializes a number of variables. After that, the values in the
globals class never change. It's a lot easier to maintain this type of
code than to passing the same variables from function to function to
function.

On the other hand, if multiple functions were willy-nilly changing
global variables then globals would be a maintenance nightmare.

To unilaterally state that globals are always "evil" borders on a
subjective religious conviction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top