I'm a python/programming newbie and maybe my question has no sense at all.
我是一个python /编程新手,也许我的问题根本就没有意义。
My problem is that I can't get a variable to be global if it is dynamic, I mean I can do this:
我的问题是,如果它是动态的,我不能让变量成为全局变量,我的意思是我可以这样做:
def creatingShotInstance():
import movieClass
BrokenCristals = movieClass.shot()
global BrokenCristals #here I declare BrokenCristals like a global variable and it works, I have access to this variable (that is a shot class instance) from any part of my script.
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()#it returns me 'BrokenCristals'
but if instead of doing that I declare a string variable like this:
但如果不是这样做,我声明一个像这样的字符串变量:
def creatingShotInstance():
import movieClass
a = 'BrokenCristals'
vars()[a] = movieClass.shot()
global a #this line is the only line that is not working now, I do not have acces to BrokenCristals class instance from other method, but I do have in the same method.
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()#it returns me 'BrokenCristals is not defined'
I tried this :
我试过这个:
global vars()[a]
and this:
和这个:
global eval(a)
but It gives me an error. What should I do?
但它给了我一个错误。我该怎么办?
3 个解决方案
#1
7
Instead of a dynamic global variable, use a dict:
使用dict而不是动态全局变量:
movies = {}
a = 'BrokenCristals'
movies[a] = movieClass.shot()
movies[a].set_name(a)
# etc
#2
10
For completeness, here's the answer to your original question. But it's almost certainly not what you meant to do -- there are very few cases where modifying the scope's dict
is the right thing to do.
为了完整起见,这是您原始问题的答案。但这几乎肯定不是你的意思 - 很少有情况下修改范围的dict是正确的。
globals()[a] = 'whatever'
#3
1
The global keyword specifies that a variable you're using in one scope actually belongs to the outer scope. Since you do not have nested scopes in your example, global doesn't know what you're trying to do. See Using global variables in a function other than the one that created them
global关键字指定您在一个范围中使用的变量实际上属于外部范围。由于您的示例中没有嵌套作用域,因此global不知道您要执行的操作。请参阅在创建它们的函数之外的函数中使用全局变量
#1
7
Instead of a dynamic global variable, use a dict:
使用dict而不是动态全局变量:
movies = {}
a = 'BrokenCristals'
movies[a] = movieClass.shot()
movies[a].set_name(a)
# etc
#2
10
For completeness, here's the answer to your original question. But it's almost certainly not what you meant to do -- there are very few cases where modifying the scope's dict
is the right thing to do.
为了完整起见,这是您原始问题的答案。但这几乎肯定不是你的意思 - 很少有情况下修改范围的dict是正确的。
globals()[a] = 'whatever'
#3
1
The global keyword specifies that a variable you're using in one scope actually belongs to the outer scope. Since you do not have nested scopes in your example, global doesn't know what you're trying to do. See Using global variables in a function other than the one that created them
global关键字指定您在一个范围中使用的变量实际上属于外部范围。由于您的示例中没有嵌套作用域,因此global不知道您要执行的操作。请参阅在创建它们的函数之外的函数中使用全局变量