使所有变量全局化 [英] Making all variables global
问题描述
我的Python脚本中有一个函数,其中创建了多个变量,并且我想在其他函数中使用它们。我想过为此使用 global
,但我认为这是不正确的方式。
任何人都会告诉我什么是在其他函数中为函数创建变量的最佳方法? 解决方案
将变量组织到< class 。在一个函数中实例化这个类,然后将这个实例传递到你需要的任何地方。
经验法则:如果在某个时候制作全球化的东西似乎是一个很好的解决方案,不行。总是有更好的办法。
I have a function in my Python script where multiple variables are created, and I want to use them in other functions. I thought of using global
for this but I thought it would be the incorrect way to do so.
So can anyone tell me what would be the best way to create variables in a function for other functions?
Organize the variables into a class. Instantiate the class in one function and then pass the instance to wherever you need it.
Rule of thumb: If making something global seems like a good solution at some point, don't do it. There is always a better way.
这篇关于使所有变量全局化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!