1

I recently learned about python vars() and it's ability to interpret a string as a variable. I want to be able to programmatically call on variables/dictionaries by adding/subtracting and using strings. Here is code that I wrote to test out how it works:

    var1 = 3
    var2 = 5
    var3 = 7
    #....

    var_string = "var1"
    x = vars()[var_string] - 1
    print (x)

If I run the code, as expected it interprets the string "var1" as the already defined global variable var1 = 3. X inherits var1's value and subtracts one, printing out 2 as desired. However, the moment I take the same code and put it into a function, it stops working and returns a key error.

    var1 = 3
    var2 = 5
    var3 = 7
    #....

    def my_Function():
        var_string = "var1"
        vars()[var_string]
        x = vars()[var_string] - 1
        print (x)
    my_Function()

KeyError: 'var1'. It appears like because it's in a function vars() is no longer using global variables and is maybe trying to look through local variables? But adding "global var1" to the top of my function doesn't seem to fix it.

I know one way to make it work would be to append my variable/dictionary to a list, ditch vars() entirely, and just call list[#]. But is it possible to avoid having a second copy of my variable? Pretend that I have a dictionary that's gigabytes in size and I'm trying to save resources. Is it possible? Is there maybe an alternative to vars() that I should be using? Thanks.

adamboy7
  • 23
  • 4
  • 1
    ``vars()`` is not intended to explicitly access globals. Use ``globals()`` for that. – MisterMiyagi Jul 17 '20 at 06:58
  • Does this answer your question? [what is the purpose of the python builtin vars() function?](https://stackoverflow.com/questions/54047163/what-is-the-purpose-of-the-python-builtin-vars-function) – DYZ Jul 17 '20 at 06:58
  • Take note that the global namespace is *also* a dictionary, so there is no harm using a dictionary instead (many small may even be more space efficient than one large). Binding a value to separate names does not make a copy, so there is very little cost in aliasing – variables cost a few bytes and are entirely negligible if the data is gigabytes in size. [This question](https://stackoverflow.com/questions/13530998/are-python-variables-pointers-or-else-what-are-they) may be relevant to understand the cost of variables versus objects. – MisterMiyagi Jul 17 '20 at 07:05
  • Thank you MisterMiyagi! That was exactly what I needed! – adamboy7 Jul 17 '20 at 07:23

1 Answers1

0

Special thanks to MisterMiyagi for the answer, they commented but didn't post an answer for me to select. The alternative function to vars() I was looking for was globals(). The issue is that what I was trying to do wasn't quite how vars() was meant to be used.

    var1 = 3
    var2 = 5
    var3 = 7
    #....

    def my_Function():
        var_string = "var1"
        x = globals()[var_string] - 1
        print (x)

It is also possible to accomplish a similar outcome by using another dictionary without spending excessive resources thanks to how namespace works, not quite the focus of the question but was touched upon.

adamboy7
  • 23
  • 4