I am new to python / programming, and maybe my question doesn't make any sense.
My problem is that I cannot force a variable to be global if it is dynamic, I mean that I can do this:
def creatingShotInstance():
import movieClass
BrokenCristals = movieClass.shot()
global BrokenCristals
BrokenCristals.set_name('BrokenCristals')
BrokenCristals.set_description('Both characters goes through a big glass\nand break it')
BrokenCristals.set_length(500)
Fight._shots.append(BrokenCristals)
def accesingShotInstance():
import movieClass
return BrokenCristals.get_name()
but if instead I declare a string variable as follows:
def creatingShotInstance():
import movieClass
a = 'BrokenCristals'
vars()[a] = movieClass.shot()
global a
eval(a+".set_name('"+a+"')")
eval(a+".set_description('Both characters goes through a big glass\nand break it')")
eval(a+".set_length(500)")
Fight._shots.append(vars()[a])
def accesingShotInstance():
import movieClass
return BrokenCristals.get_name()
I tried this:
global vars()[a]
and this:
global eval(a)
but it gives me an error. What should I do?
source
share