Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python 2.6 Global Variables

Reply
Thread Tools

Python 2.6 Global Variables

 
 
mattofak
Guest
Posts: n/a
 
      10-29-2009
Hi All;

I'm new to Python and moving from C, which is probably a big source of
my confusion. I'm struggling with something right now though and I
hope you all can help.

I have a global configuration that I would like all my classes and
modules to be able to access. What is the correct way to do this?

Thanks;
Matthew Walker
 
Reply With Quote
 
 
 
 
Ronn Ross
Guest
Posts: n/a
 
      10-29-2009
Inside the method that you want to use the var prefix the first
instance with global. For example: global my_var. Then you can use the
var like normal in the method. Good luck

On Oct 28, 2009, at 20:50, mattofak <(E-Mail Removed)> wrote:

> Hi All;
>
> I'm new to Python and moving from C, which is probably a big source of
> my confusion. I'm struggling with something right now though and I
> hope you all can help.
>
> I have a global configuration that I would like all my classes and
> modules to be able to access. What is the correct way to do this?
>
> Thanks;
> Matthew Walker
> --
> http://mail.python.org/mailman/listinfo/python-list

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      10-29-2009
> On Oct 28, 2009, at 20:50, mattofak <(E-Mail Removed)> wrote:
>> Hi All;
>>
>> I'm new to Python and moving from C, which is probably a big source of
>> my confusion. I'm struggling with something right now though and I
>> hope you all can help.
>>
>> I have a global configuration that I would like all my classes and
>> modules to be able to access. What is the correct way to do this?


On Wed, Oct 28, 2009 at 6:02 PM, Ronn Ross <(E-Mail Removed)> wrote:
> Inside the method that you want to use the var prefix the first instance
> with global. For example: global my_var. Then you can use the var like
> normal in the method. Good luck


Note that without a global declaration, functions can still read
global variables and modify the objects associated with them, but will
be unable to re-bind (i.e. reassign) entirely different objects to
global variables; the `global` statement just permits them to rebind
global variables to new values.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
Benjamin Kaplan
Guest
Posts: n/a
 
      10-29-2009
On Wed, Oct 28, 2009 at 8:50 PM, mattofak <(E-Mail Removed)> wrote:
> Hi All;
>
> I'm new to Python and moving from C, which is probably a big source of
> my confusion. I'm struggling with something right now though and I
> hope you all can help.
>
> I have a global configuration that I would like all my classes and
> modules to be able to access. What is the correct way to do this?
>


Make a separate module with all the config stuff in it, and import
that module everywhere you need it. Just make sure you do "import
settings" and not "from settings import *". The behavior is different.
In the first case, one instance of the module is shared among every
module that imports it, so any changes you make will appear in all
modules. IN the second case, the current values in settings.py are
copied into the local namespace. Changes made in one module won't
appear in the other modules.

> Thanks;
> Matthew Walker
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-29-2009
Ronn Ross a Úcrit :
(please don't top-post - fixed)
> On Oct 28, 2009, at 20:50, mattofak <(E-Mail Removed)> wrote:
>
>> Hi All;
>>
>> I'm new to Python and moving from C, which is probably a big source of
>> my confusion. I'm struggling with something right now though and I
>> hope you all can help.
>>
>> I have a global configuration that I would like all my classes and
>> modules to be able to access. What is the correct way to do this?
>>


> Inside the method that you want to use the var prefix the first
> instance with global. For example: global my_var. Then you can use the
> var like normal in the method. Good luck



Wrong, and wrong again.

1/ you don't have to use the "global" statement to access a "global"
name - only if you plan on rebinding it (which is more often than not a
bad idea but that's another problem)

2/ in Python, "global" really means "module-level" - there's nothing
like a "true" global namespace.
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      10-29-2009
On Thu, Oct 29, 2009 at 3:25 AM, VYAS ASHISH M-NTB837
<(E-Mail Removed)> wrote:
>
> Dear all
>
> How do I write a code that gets executed 'every x' minutes?
>
>
>
> I know how to do it 'after x' minutes, I do the following:
>
> def doAtTimerFire():
> ┬* ┬* ┬* ┬*""" The things I want to do 'after x' minutes go here. """
>
> And then from main code, I do this:
>
> tmr = threading.Timer(timeInSeconds, doAtTimerFire)
> tmr.start()
>
>
>
> Please help.
>
> Regards,
> Ashish Vyas


Please exercise some basic mailinglist etiquette and start a new
thread, don't hijack an existing one with a completely unrelated
question.
New threads are started by emailing your post to
http://www.velocityreviews.com/forums/(E-Mail Removed) rather than replying to a message on an
existing topic.

Regards,
Chris
 
Reply With Quote
 
AK Eric
Guest
Posts: n/a
 
      10-29-2009
> 2/ in Python, "global" really means "module-level" - there's nothing
> like a "true" global namespace.


Isn't that __main__?


import __main__
__main__.foo = "asdfasdf"

print foo
# asdfasdf

Not advocating, but it does serve the purpose.
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      10-29-2009
AK Eric wrote:
>> 2/ in Python, "global" really means "module-level" - there's nothing
>> like a "true" global namespace.
>>

>
> Isn't that __main__?
>
>
> import __main__
> __main__.foo = "asdfasdf"
>
> print foo
> # asdfasdf
>
> Not advocating, but it does serve the purpose.
>
>

Good that you're not advocating it, because IMHO it's bad practice to
have circular import dependencies. By using the __main__ alias, you
avoid the worst problems, but that just means the others are more subtle.

You can make it safe, by carefully avoiding certain constructs. But
it's still fragile, in that a minor change in one module of the loop can
cause either an exception or unexpected behavior.

DaveA
 
Reply With Quote
 
AK Eric
Guest
Posts: n/a
 
      10-29-2009
> Good that you're not advocating it, because IMHO it's bad practice to
> have circular import dependencies. *By using the __main__ alias, you
> avoid the worst problems, but that just means the others are more subtle.


I figured I'd get that kind of response, not that it's incorrect
Great power\great responsibility\etc.

As I understand it, when you enter Python statements at the
interactive prompt, it's adding the result directly to ___main___
(which for lack of a better term I like to call 'universal' scope...
rolls off the tongue better than 'doubleunderscore main
doubleunderscore'):

>>> foo = 23
>>> import __main__
>>> print __main__.foo

23

While this might not be the most common way of working for most people
(I'm guessing most folks are in a nice cozy IDE), people working this
way are mucking about in the 'universal' scope without (possibly) even
knowing it.
 
Reply With Quote
 
Benjamin Kaplan
Guest
Posts: n/a
 
      10-29-2009
On Thu, Oct 29, 2009 at 7:11 PM, AK Eric <(E-Mail Removed)> wrote:
>> Good that you're not advocating it, because IMHO it's bad practice to
>> have circular import dependencies. *By using the __main__ alias, you
>> avoid the worst problems, but that just means the others are more subtle..

>
> I figured I'd get that kind of response, not that it's incorrect
> Great power\great responsibility\etc.
>
> As I understand it, when you enter Python statements at the
> interactive prompt, it's adding the result directly to ___main___
> (which for lack of a better term I like to call 'universal' scope...
> rolls off the tongue better than 'doubleunderscore main
> doubleunderscore'):
>
>>>> foo = 23
>>>> import __main__
>>>> print __main__.foo

> 23
>
> While this might not be the most common way of working for most people
> (I'm guessing most folks are in a nice cozy IDE), people working this
> way are mucking about in the 'universal' scope without (possibly) even
> knowing it.
> --


Or, you could use any other random module for this like, say, a module
made specifically for this purpose and given a name like "config.py"
or "settings.py" or something like that which describes what you're
using it for. You don't have a "universal" scope- it's the
module-level scope of the script that is actually run (or the
interactive interpreter in this case).
 
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
Initialize global variable before any other global variables jubelbrus C++ 5 07-20-2007 06:38 PM
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
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM
Global variables - application variables vs include file mark4asp ASP General 1 09-03-2003 01:30 PM



Advertisments