Is it a good idea to dynamically create variables?

后端 未结 5 1928
故里飘歌
故里飘歌 2020-12-06 23:58

I recently found out how to dynamically create variables in python through this method:

vars()[\'my_variable\'] = \'Some Value\'

Thus creat

5条回答
  •  鱼传尺愫
    2020-12-07 00:20

    There is no guarantee that vars()['myvariable'] = 'Some value' and my variable = 'Some value' have the same effect. From the documentation:

    Without an argument, vars() acts like locals(). Note, the locals dictionary is only useful for reads since updates to the locals dictionary are ignored.

    This code is simply wrong.

提交回复
热议问题