P.S. I am not sure why this the interpreter in this site does not recognize vars()... however, it appears that the interpreter in W3Schools works just fine with vars.
I am using vars() and globals(). I am not sure whether this is a good practice, do let me know if it isn't!
This works
Any idea why? Thanks in advance!
I am using vars() and globals(). I am not sure whether this is a good practice, do let me know if it isn't!
This works
x=10
y=5
xy_wsp = [('x','x'),('y','y')]
lst = [lv for v,lv in xy_wsp]
vars()[lst[0]]For-looping it,new_lst = []
for lv in lst:
new_lst.append(vars()[lv])However, if I use in a list interpretation,[vars()[lv] for lv in lst]Then I get
Error:KeyError: 'x'Somehow, [globals()[lv] for lv in lst]works.
Any idea why? Thanks in advance!
