python - referencing a variable in another function? -
I am an experienced TCL developer and write my own processes to help myself. I call it a proc i callVar, print out the value of the variable in a certain format, so I know that it is variable and what is the value "set foo 1; putVar foo" result 'foo = "1" I want to do the same thing in Python, but I can not find the answer :( In TCL I use the up command because when I pass the name of the variable, then I can see the value at the top. .
I understand that an advanced light in the dragon There is no mechanism of r, but with its introspection capability, I would think that this is something trivial, but I am not getting the answer anywhere.
It seems that this should work:
and expanded:
def foo (): y = "Hello World" printwor ("y") foo () y = "Hello World "
You can, but you should not do so if your code is correct If is needed , then there is something wrong with your design, and you should probably use the dictionaries or objects explicitly passed with the appropriate attributes. But, to answer the question directly: You can use sys._getframe . Just know that it can not be portable on Python implementation (i.e. only works on CPIthon): def get_object (name): return sys._getframe (1) .f_locals [name ] X = 'Foo bar' print get_object ('x') def foo (): y = 'baz baf' print get_object ('y') foo ()
Comments
Post a Comment