Is it a good idea to dynamically create variables?

自古美人都是妖i 提交于 2019-11-26 21:58:28

问题


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

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

Thus creating the variable my_variable.

My question is, is this a good idea? Or should I always declare the variables ahead of time?


回答1:


I think it's preferable to use a dictionnary if it's possible:

vars_dict = {}
vars_dict["my_variable"] = 'Some Value'
vars_dict["my_variable2"] = 'Some Value'

I think it's more pythonic.




回答2:


This is a bad idea, since it gets much harder to analyze the code, both for a human being looking at the source, and for tools like pylint or pychecker. You'll be a lot more likely to introduce bugs if you use tricks like that. If you think you need that feature at some time, think really hard if you can solve your problem in a simpler and more conventional way. I've used Python for almost 20 years, and never felt a need to do that.

If you have more dynamic needs, just use a normal dictionary, or possibly something like json.

One of the great things with Python, its dynamic nature and good standard collection types, is that you can avoid putting logic in text strings. Both the Python interpreter, syntax highlighting in your IDE, intellisense and code analysis tools look at your source code, provides helpful suggestions and finds bugs and weaknesses. This doesn't work if your data structure or logic has been hidden in text strings.

More stupid and rigid languages, such as C++ and Java, often makes developers resort to string based data structures such as XML or json, since they don't have convenient collections like Python lists or dicts. This means that you hide business logic from the compiler and other safety checks built into the language or tools, and have to do a lot of checks that your development tools would otherwise do for you. In Python you don't have to do that ... so don't!




回答3:


Pros:

  • adds another level of indirection, makes the environment more dynamic
    • in particular, allows to avoid more code duplication

Cons:

  • not applicable for function namespaces (due to optimization)
  • adds another level of indirection, makes the environment more dynamic
    • "lexical references" are much harder to track and maintain
      • if created names are arbitrary, conflicts are waiting to happen
      • it's hard to find the ins and outs in the code base and predict its behaviour
      • that's why these tricks may upset code checking tools like pylint
  • if variables are processed in a similar way, they probably belong together separately from others (in a dedicated dict) rather than reusing a namespace dict, making it a mess in the process

In brief, at the abstraction level Python's language and runtime features are designed for, it's only good in small, well-defined amounts.




回答4:


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.




回答5:


I don't see what would be the advantage of it, also would make your code harder to understand. So no I don't think it is a good idea.



来源:https://stackoverflow.com/questions/35458691/is-it-a-good-idea-to-dynamically-create-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!