tags:

views:

60

answers:

1

I am using a large list of variables inside some definitions and classes (mainly because I want to be able to use the code-folding feature of pydev). Is there any constructor I can use on a definition or class to make its variables automatically considered globals?


This is an example of what I did after following some of the recommendations provided on the comments:

From:

img_globe       = os.path.join(set_img_dir, 'img_globe.png')
img_help        = os.path.join(set_img_dir, 'img_help.png')
img_exit        = os.path.join(set_img_dir, 'img_exit.png')
img_open        = os.path.join(set_img_dir, 'img_open.png')
img_tutorial    = os.path.join(set_img_dir, 'img_tutorial.png')
img_save        = os.path.join(set_img_dir, 'img_save.png')
img_site        = os.path.join(set_img_dir, 'img_site.png')

... (long, long list)

To:

varies = {}
dirList=os.listdir(set_img_dir)
for fname in dirList: 
    varies[fname.split(".")[0]] = os.path.join(set_img_dir, fname)
A: 

Although you should not do this and the solution you are looking for is not as simple as you might think, here is a very simple example of how you might take the local variables from within a function and make them global:

def make_locals_globals():
    """This is just bad"""
    foo = 1
    bar = 2

    locals_dict = locals()
    globals_dict = globals()

    print 'Locals:', locals_dict

    for varname, varval in locals_dict.items():
        print 'Setting global: %s=%s' % (varname, varval)
        globals_dict[varname] = varval

if __name__ == '__main__':
    make_locals_globals()

    print '\nGlobals:'
    print 'foo=', foo
    print 'bar=', bar
jathanism
I thought it was a simple one-liner problem. Besides expecting some drawbacks in efficiency, I was very surprised to learn (from the comments) that this behavior is so heavily discouraged. Thanks for your help.
relima
The reason you should not do this is because it enforces bad practice of expecting all variable to be global. Once that happens it becomes hard (if not impossible) to track which operation is referencing what variable, and it is always too easy to overwrite a global in the local scope, unless you are OCD with your variable naming. Python has distinct and elegant scoping rules that work to your benefit if you let them.
jathanism
You are right, I have been pretty obsessive with the naming of my variables for this reason. I am going to try to make use of dictionaries.
relima
@relima: Maybe you should try some other IDEs (although I've heard pydev is one of the best). FWIW the text editor I use most of the time will let me hide any lines I wish (it's also Python syntax-aware).
martineau
You could one-liner it with `globals().update(locals())`. With great power comes great... gas.
jathanism
@relima it's not simply a matter of being discouraged, there are documented reasons why this is problematic if you are not careful. Just do a quick google with the keywords `global` `variable` `evil` or `bad` and it should provide you with a few cases. An obvious one would be a function affecting global variables which then affect other functions in an undesired way.
Unode
@jathanism: Good answer, but I doubt that your one-liner in the comments would help him hide and show the variables in his IDE.
martineau
Nope! There no winners at code golf.
jathanism
martineau