shortcut for large amount of global var declarations?

A

Alex Hall

Hi all,
I am sorry if this is the second message about this you get; I typed
this and hit send (on gmail website) but I got a 404 error, so I am
not sure if the previous message made it out or not.
Anyway, I have about fifteen vars in a function which have to be
global. Is there a faster and more space-efficient way of doing this
than putting each var on its own line; that is, fifteen "global
varName" declarations? Thanks.
 
J

Jon Clements

Hi all,
I am sorry if this is the second message about this you get; I typed
this and hit send (on gmail website) but I got a 404 error, so I am
not sure if the previous message made it out or not.
Anyway, I have about fifteen vars in a function which have to be
global. Is there a faster and more space-efficient way of doing this
than putting each var on its own line; that is, fifteen "global
varName" declarations? Thanks.

About 15 that *need* to be global; smells bad to me. However, you can
amend a function's .func_globals attribute. Just wouldn't be going
there myself. Why do you have this many? What's your use case?

Jon.
 
A

Alex Hall

About 15 that *need* to be global; smells bad to me. However, you can
amend a function's .func_globals attribute. How do you do this exactly?
Just wouldn't be going there myself. Why do you have this many? What's your
use case?
They are in a "setOptions" function in config.py, which reads from an
ini file and sets up all options for the program. All other files then
can read these options. For example, anyone else can import config,
then, if they are rounding a number, they can know how many places to
round to by looking at config.rnd. I have everything in a function
instead of being initialized upon importing since I offer a function
to reload the ini file if the user changes something. Eventually I
will have a gui for setting options, and that gui, to save options,
will write an ini file then call setOptions so all options are then
reset according to the newly created ini. If I do not make everything
in setOptions global, no other file seems able to read it; I get an
exception the first time another file tries to access a setting.
Thanks.
 
J

Jon Clements

How do you do this exactly?> Just wouldn't be going there myself. Why do you have this many? What's your

They are in a "setOptions" function in config.py, which reads from an
ini file and sets up all options for the program. All other files then
can read these options. For example, anyone else can import config,
then, if they are rounding a number, they can know how many places to
round to by looking at config.rnd. I have everything in a function
instead of being initialized upon importing since I offer a function
to reload the ini file if the user changes something. Eventually I
will have a gui for setting options, and that gui, to save options,
will write an ini file then call setOptions so all options are then
reset according to the newly created ini. If I do not make everything
in setOptions global, no other file seems able to read it; I get an
exception the first time another file tries to access a setting.
Thanks.

Umm, okay at least now we know the context.

Similar to what James suggested just have a dict object in your config
module called 'settings' or something and access that. I still prefer
the "giveth rather than taketh" approach though. But heck, if it
works, who cares?

Jon.
 
S

Steven D'Aprano

Hi all,
I am sorry if this is the second message about this you get; I typed
this and hit send (on gmail website) but I got a 404 error, so I am not
sure if the previous message made it out or not. Anyway, I have about
fifteen vars in a function which have to be global. Is there a faster
and more space-efficient way of doing this than putting each var on its
own line; that is, fifteen "global varName" declarations? Thanks.

global google, for_, global_, variables, considered, harmful
 
L

Lawrence D'Oliveiro

Alex Hall said:
... I have about fifteen vars in a function which have to be
global.

Why not make them class variables, e.g.

class my_namespace :
var1 = ...
var2 = ...
#end my_namespace

def my_function(...) :
... can directly read/assign my_namespace.var1 etc here ...
#end my_function

Also has the benefit of minimizing pollution of the global namespace.
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top