I have a Globals class.
In it, I have a variable defined something like this:
remote_device_e nabled = 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
misunderstandin g?
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 9 999
Newbie too. I think you shoud qualify Global with the module name.
On 10/23/08, Pat <Pa*@junk.netwr ote:
I have a Globals class.
In it, I have a variable defined something like this:
remote_device_e nabled = 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>.Global s.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
misunderstandin g?
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
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_e nabled = 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
misunderstandin g?
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.
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 misunderstandi ng?
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_e nabled = bool
---my initialize.py file:
from myGlobals import *
def initialize():
myGlobals.remot e_device_enable d = True
---my main.py file:
import from myGlobals import *
RDE = myGlobals.remot e_device_enable d
def main():
if RDE: # this will not give me the correct value
process_device( )
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_e nabled = 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
misunderstandin g?
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
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_e nabled = 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.remot e_device_enable d = True
Sets the class attribute remote_device_e nabled 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.remot e_device_enable d
Creates a local name RDE which takes its initial value from the class
attribute remote_device_e nabled. 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
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_e nabled = 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.remot e_device_enable d = 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.remot e_device_enable d
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>
In message <01************ **********@news .astraweb.com>, Steven D'Aprano
wrote:
Why is it a class attribute instead of an instance attribute?
Singleton class.
At 2008-10-24T01:08:12Z, Pat <Pa*@junk.netwr ites:
---myGlobals.py file:
class myGlobals():
remote_device_e nabled = bool
---my initialize.py file:
from myGlobals import *
def initialize():
myGlobals.remot e_device_enable d = True
---my main.py file:
import from myGlobals import *
RDE = myGlobals.remot e_device_enable d
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_e nabled = None
---my initialize.py file:
import myGlobals
def initialize():
myGlobals.remot e_device_enable d = True
---my main.py file:
import myGlobals
import initialize
initialize.init ialize()
def main():
if myGlobals.remot e_device_enable d:
process_device( )
--
Kirk Strauser
The Day Companies
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 scope they meant public/protected/private access modifiers :)
Obviously all members of the base privately inherited class will be
private, and that was my answer. However, the teacher checked my
answers when I handed in, and the problem was that I had...
|
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 threads of a multithreaded
program using embedded SQL. Since the threads do not need to share
transaction scopes, the sqleAttachToCtx family of APIs do not seem to be
necessary.
<quote>
In the default implementation of threaded applications against...
|
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;
|
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;
|
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 shows the alert. What is the essential
reason?
function () { alert('hi mom'); }();
function () { alert('hi dad'); }(8);
var x=function () { alert('hi bro'); }();
| |
by: Cylix |
last post by:
I just know the form like:
myObject.prototype.linkFade = function(link, doShow) {
....
....
....
}
-----------------------------------------------------------------------------------------------------------------------------------
myObject.prototype.linkFade = function(link, doShow) { with (this) {
....
....
|
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 case a single object is created using object
literal notation and both the get and __Private methods are availabile
for use and no closure is created.
ABC.Util.SomeObject =
{
|
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 call
with "SomeObj".
Can someone describe why this is happening?
var SomeObj = {
doSomething : function()
{
|
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 global var
private void method1(int myInt){
int x; //local var
etc...
|
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;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |