git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Use global, or not


On 29/06/19 1: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:
>      global global_dict
...

> Is that an acceptable way to do this?


If it works, isn't that the largest part of "acceptable"?

In each case, (previously discussed and/or below) you are taking 
advantage of "namespaces" to keep one 'lot' of values separated and 
distinct from others - as well as finding a means of 'passing them 
around'. The other half of the considerations is how the values are 
being retrieved/utilised within the mainline code.

An alternative might be to use a class. Then accessing a single value 
becomes instance.attribute, but when you have need for several 
attribute's values or to compute a value from several attributes, an 
instance.method() may simplify things.

I use the above idea for a 'configuration' class which has values built 
from constants/defaults, plus command-line arguments, etc (as 
appropriate to the application and user-needs).

Another approach might be to use a module. After import module as 
module_name, single-value access becomes module_name.cancelled_report (etc).

-- 
Regards =dn