On Mon, Mar 12, 2012 at 3:27 PM, Brian Brown <[email protected]> wrote:
> Thanks for the replies everyone. > I guess Python just doesn't have a > "declare-variable-as-global-for-whole-module" feature. > > (I don't generally have problems with variable overlap because I'm very > careful with naming my global variables.) > > (I just remembered though-- for some reason, > list-type variables' individual values/items can be easily accessed > throughout the whole module even if: it was just "global"ed once inside > the function it was created.) > > But I just wish I could create variables (of ANY TYPE) which are > universally acknowledged by the WHOLE module. > > Many of my variables' names are re-stating the exact value they hold, so I > rarely have any confliction with names: > > filename_of_current_level = 'desert.txt'' > Font__New_Times_Roman_size_20 = ... > pi_divided_by_180 = math.pi() / 180 > > etc. > The names are just according to what values they hold which-- are all > unique anyways! > And when the number of variables start reaching the hundreds, it just gets > really crazy with "global this global that" ALL OVER my code, again and > again . . . > : ( > I have to keep remembering to global every other variable in any function > that uses my main game variables. > Listen carefully (I say that because this has already been explained to you multiple times). You don't need to use the global keyword to access a global variable in a function if you don't assign to that variable within the function. Two of your three examples (Font__New_Times_Roman_size_20 and pi_divided_by_180) are extremely likely to fall into this category every time they appear in a function. I think if you looked through your code carefully, you'd see that you need far fewer global keywords than you actually think you do. -Christopher
