动态创建变量是一个好主意吗? [英] Is it a good idea to dynamically create variables?
本文介绍了动态创建变量是一个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我最近发现了如何通过此方法在python中动态创建变量:
I recently found out how to dynamically create variables in python through this method:
vars()['my_variable'] = 'Some Value'
因此创建了变量my_variable
.
我的问题是,这是个好主意吗?还是应该总是提前声明变量?
My question is, is this a good idea? Or should I always declare the variables ahead of time?
推荐答案
我认为最好使用字典:
vars_dict = {}
vars_dict["my_variable"] = 'Some Value'
vars_dict["my_variable2"] = 'Some Value'
我认为它更具有Python性.
I think it's more pythonic.
这篇关于动态创建变量是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文