0

I need to print variable names, so I did:

def get_variable_name(x):
    local_matches = [k for k, v in locals().items() if v is x]
    global_matches = [k for k, v in globals().items() if v is x]

    print(local_matches, global_matches)
    return global_matches[0]

a = 5
print(get_variable_name(a))

# output
# (['x'], ['a'])
# a

That's now perfect, but now I need to use this get_variable_name function in many places in my project. So I moved this function to utils.py, and we can now import it and use it like:

from scripts.utils import get_variable_name
a = 5
print(get_variable_name(a))

# output
# (['x'],[])
# ! can't access index 0 exception
# ! since the list is empty

Clearly, in the former, a is found in the global var stack and in the latter it's not, since when it's inside of utils.py executing this functions, globals() does not contain a, since it is not declared in the global scope (of the file).

So we got locals().items() to give local vars, and globals().items() for global vars, How do I access inter-file scope variable stack?


UPDATE:

In another SO question, mentioned in the comments, they are dealing with variables defined in the global scope in file1 and this will not be available in file2 because, file1 imports only modules of file2.

But here, I am sending these variables to file2, and obviously, values of which itfile2 has access to, but how do I access the names of the arguments passed? (And I don't see a security or dependency flaw associated with this, as it already has access to the variable, I just want their names)

Saravanabalagi Ramachandran
  • 8,551
  • 11
  • 53
  • 102
  • Linked: https://stackoverflow.com/a/6486008/3125070 – Saravanabalagi Ramachandran Jul 26 '18 at 14:39
  • I think i found a post that has answered the same question, https://stackoverflow.com/questions/3400525/global-variable-from-a-different-file-python – Max Jul 26 '18 at 14:40
  • It is not recommended. Can create lots of bugs and exceptions if you do. You should read more about cyclical dependency. – Max Jul 26 '18 at 14:46
  • @Max updated the qustion – Saravanabalagi Ramachandran Jul 26 '18 at 14:51
  • As you said you are sending the value of the parameter `a` to `file2`, Thus the value of `x` in `file2` is the same as the value of `a` in `file1`. The function works perfectly fine, it dosent know that there is another parameter that is called a, you pass the value, and so it knows only of the existing of `x`. You are not letting `file2` manipulate variable `a` directly, which is why it does not recognizes any additional params. – Max Jul 26 '18 at 14:57
  • @Max Not all the args are passed by value, sometimes, internally python will take the reference of the data structure, for eg. mutation happens on the same list object. Perhaps this needs a little more digging deep? – Saravanabalagi Ramachandran Jul 26 '18 at 17:21
  • There are mutable and immutable objects. In short, if you pass a mutable object, you pass a reference to it. In this case it dosent matter, the point is that you *pass* an argument to a function. You dont create a new parameter in `file2`, you only pass a value in an already existing parameter, that why `gloabl()` wont show you two params. As said above, if you want it to catch the params in `file1`, you have to use the import * state in `file2` which will create cyclical dependency. – Max Jul 26 '18 at 17:25

0 Answers0