Use global, or not

Rob Gaddi rgaddi at highlandtechnology.invalid
Fri Jun 28 12:58:52 EDT 2019


On 6/28/19 6:44 AM, Cecil Westerhof wrote:
> I have written a GUI program where I have quit a few global variables.
> I did not like this, so I now use one global dict. Something like:
[snip]
>      global_dict          = {
>          'messages': messages,
>          'progress': progress,
>          'window':   window,
>      }
> 
> And in the program do things like:
>      global_dict['progress']['value'] += 1
>      global_dict['window'].update_idletasks()
>          global_dict['window'].update_idletasks()
> 
> and:
>      messagebox.showwarning(global_dict['titles']['warning'],
>                             global_dict['messages']['nofiles'])
> 
> 
> Is that an acceptable way to do this?
> 

It works.  I guess it makes your use of items in the global namespace inherently 
intentional rather than allowing accidental globals.  I'm not sure how much 
value it adds though.

That said, I've got a whole bunch of programs that all use a dict called 
Registry that they all import from a shared package; making them program-wide 
globals.  I use it for a few pieces of static information ('application', 
'version') as well as to pass device handles around, since an open connection to 
a given piece of physical hardware is an inherently global thing.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list