shortcut for large amount of global var declarations?

Jon Clements joncle at googlemail.com
Sat May 8 13:04:30 EDT 2010


On 8 May, 16:03, Alex Hall <mehg... at gmail.com> wrote:
> On 5/8/10, Jon Clements <jon... at googlemail.com> wrote:
>
> > On 8 May, 15:08, Alex Hall <mehg... at gmail.com> wrote:
> >> 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.
>
> >> --
> >> Have a great day,
> >> Alex (msg sent from GMail website)
> >> mehg... at gmail.com;http://www.facebook.com/mehgcap
>
> > 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.
>
>
>
> > Jon.
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> --
> Have a great day,
> Alex (msg sent from GMail website)
> mehg... at gmail.com;http://www.facebook.com/mehgcap

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.



More information about the Python-list mailing list