0

There are instances where a variable is practical to be declared as visible throughout the script (or be global) to avoid having to pass it as an argument, among other reasons. In my case, it is often a log file reference so that it can be written to from anywhere in the script without being explicitly passed but I am sure there are many other uses.

I have accomplished this in different ways:

#!/bin/python3

################################################
# script var declaration outside classes and functions
################################################

# method 1
global globalVarOne
# method 2
globalVarTwo = None

################################################
# class SomeClass
################################################

class SomeClass(object):

    #both globalVarOne and globalVarTwo 
    #are equally visible

################################################
# def someFunction
################################################

def someFunction(argOne, argTwo):

    #both globalVarOne and globalVarTwo 
    #are equally visible

################################################
# main
################################################

if __name__ == '__main__':

    #...

What is the difference between declaring such variables as global vs just setting them to None in a statically invoked block of the script? Are there other (better) ways to accomplish the same? Also, is there a pythonic name for that part of the script, which is not a part of the main or any class or function?

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • There's no point to declaring a global at top-level. The point of `global` is in *other* scopes, to control whether it's the module-scope variable modified or a new local that's created on assignment. – Charles Duffy Mar 16 '17 at 16:33
  • so are you saying `global globalVarOne` and `globalVarOne = None` at the top do exactly the same thing? – amphibient Mar 16 '17 at 16:38
  • No, I'm saying `global globalVarOne` at the top is useless and does nothing at all. – Charles Duffy Mar 16 '17 at 16:38
  • `global globalVarOne` would be useful inside a class or a function, *before* running `globalVarOne = something` inside that same class or function. – Charles Duffy Mar 16 '17 at 16:39
  • so essentially, would you say that `globalVarOne = None` at the top is the best way to make that variable visible throughout the script? – amphibient Mar 16 '17 at 16:40
  • "visible throughout the script" doesn't require anything special. The problem that `global` is needed for isn't "visible" for reads. The problem that `global` is needed for is making something a destination for *writes*. – Charles Duffy Mar 16 '17 at 16:41
  • OK -- i typically assign value to such vars in the `main` and then the value is read elsewhere. But there are instances in which it needs to be both read and assigned everywhere – amphibient Mar 16 '17 at 16:43
  • 1
    If you assign in `main`, then you need to put `global globalVarOne` in `main`. – Charles Duffy Mar 16 '17 at 16:43
  • also, and i am aware this is a matter of personal coding style, but i like to have all the global vars declared in one place so that i can easily identify them. I know this has no syntactic function. it's more like a comment – amphibient Mar 16 '17 at 16:44
  • so then the better way than what i am doing is to forget the static block (whatever it is called) and just assign such vars as global in the main ? – amphibient Mar 16 '17 at 16:45
  • 1
    Personally, I'd do both. Assign a pre-initialization value at module scope (if you don't yet have enough information to do real initialization there), then assign an initial runtime value in your main (after a `global yourVarName` declaration there in the main function). – Charles Duffy Mar 16 '17 at 16:47
  • also -- i find your comments helpful. perhaps you can consolidate them in an answer. i will upvote it and possibly accept it if no better answer comes up – amphibient Mar 16 '17 at 16:47
  • I'm 99% certain this is a duplicate of something already asked and answered. I'll add an answer if I don't find that question. – Charles Duffy Mar 16 '17 at 16:48

0 Answers0