0

I am trying to set and get global variables from different files as

##config.py
def init():
    global CELL_NUM_INROWS
    CELL_NUM_INROWS = 10

##main.py
if __name__ == '__main__': 
    config.init()
    ######some codes here
    g = Game(diff, dim, path)
    g.start()

class Game:
    def __init__(self, diff, dim, path):
        config.CELL_NUM_INROWS = 20  ##eclipse putting red underline and saying undefined variable.

My code is working but I just wonder what is the problem here ? If I modify my variable inside the main it is not showing any red underlining. Thanks

ozturkib
  • 1,493
  • 16
  • 28
  • 2
    is CELL_NUM_INROWS also defined at the root? i.e. usually there is something like `CELL_NUM_INROWS = None` before the init somewhere. you didn't show it, but maybe you have it anyways. otherwise, it doesn't exist until you run __init__, so static analysis won't see it. – Corley Brigman Oct 30 '14 at 14:43
  • Is there a need for explicitly initializing the config, vs just letting it initialize implicitly by being parsed in module scope on first import? It seems a bit overcomplicated as it stands. If you actually do need it explicitly initialized, it would probably be a better idea to create a `Config` object, rather than trying to use the module as a pseudo-class. – Silas Ray Oct 30 '14 at 15:05
  • I dont understand entirely what you mean. You mean CELL_NUM_INROWS = None before init inside the config.py ? @Corley Brigman – ozturkib Oct 30 '14 at 15:06
  • As it stands, you can ignore the red underlining; it's a failing of static analysis tools with dynamic languages like Python. `config.CEL_NUM_INROWS` does not exist until runtime, after `config.init` is called. That said, if what I just said doesn't completely make sense to you, and/or you can't justify it, you should treat the static analysis result as a sign that there is probably an underlying design flaw in your code. – Silas Ray Oct 30 '14 at 15:09
  • @Silas Ray I want to set, change and get global variables from different python files so I am using global variables similar to this one : http://stackoverflow.com/questions/13034496/using-global-variables-between-files-in-python . Can you mention more about how can I do it by config object (with global variable changing and getting features ) – ozturkib Oct 30 '14 at 15:10
  • @Silas Ray config.init() if (config.MAZE_COMPOSING == config.GENERATE_MAZE_RANDOMLY): This one also giving the same red underline although it is just after the init and inside the same scope. So it should be there something different than static analysis – ozturkib Oct 30 '14 at 15:16
  • Static analysis will fail trying to reference any value set on the `config` module from any other module that isn't set in the `config` module's module scope. Basically, if you never have any use for passing values to the `init` of the `config` module, you're almost certainly better off just declaring all the config values in module scope to begin with. If you look at major projects that use pure Python files for configuration (Django, for instance), this is how they go about doing this. – Silas Ray Oct 30 '14 at 15:22

0 Answers0