473,396 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question about scope

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
Oct 23 '08 #1
9 984
Newbie too. I think you shoud qualify Global with the module name.

On 10/23/08, Pat <Pa*@junk.netwrote:
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 <moduleimport Globals ?
RDE = Globals.remote_device_enabled
Here,

RDE = <module>.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
--
http://mail.python.org/mailman/listinfo/python-list

--
Regards,
Lave
Oct 23 '08 #2
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...
</ot>
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.
Oct 23 '08 #3
Pat
Bruno Desthuilliers wrote:
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...
</ot>
>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.

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()

Oct 23 '08 #4
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/archiv...5/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
Oct 23 '08 #5
On Thu, 23 Oct 2008 21:08:12 -0400, Pat wrote:
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.
--
Steven
Oct 23 '08 #6
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.
</irrelevant>
---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>
Oct 24 '08 #7
In message <01**********************@news.astraweb.com>, Steven D'Aprano
wrote:
Why is it a class attribute instead of an instance attribute?
Singleton class.
Oct 24 '08 #8
At 2008-10-24T01:08:12Z, Pat <Pa*@junk.netwrites:
---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()
If you *really* want to organize your settings like this, may I suggest:

---myGlobals.py file:

# This does nothing more than create a placeholder
remote_device_enabled = None

---my initialize.py file:

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

---my main.py file:

import myGlobals
import initialize
initialize.initialize()

def main():
if myGlobals.remote_device_enabled:
process_device()
--
Kirk Strauser
The Day Companies
Oct 28 '08 #9
Pat
Steven D'Aprano wrote:
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/archiv...5/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.
Nov 1 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
6
by: Cylix | last post by:
I just know the form like: myObject.prototype.linkFade = function(link, doShow) { .... .... .... }...
4
by: emailscotta | last post by:
Below are some over simplified examples of code the create a single instance of an object and I was hoping some one could give me the pros and cons of each approach. First declaration: In this...
11
by: emailscotta | last post by:
Below I declared a basic object literal with 2 methods. The "doSomething" method is call from the "useDoSomething" method but the call is only sucessful if I use the "this" keyword or qualify the...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.