简体   繁体   English

如何使全局变量与 Python 中的 Pycharm 一起使用?

[英]How to make global variables work with Pycharm in Python?

Pycharm global variables do not seem to work. Pycharm 全局变量似乎不起作用。 Any suggestions?有什么建议?

def func():
   global a 
   print(a)

'a' is not defined I get. 'a' 没有定义我得到。

But, this works fine in Python notebook.但是,这在 Python notebook 中运行良好。 Of course I checked and 'a' is in globals() in both python notebook and pycharm.当然,我检查了 python notebook 和 pycharm 中的 globals() 中的“a”。 Any tips to resolve this, or if not optimal alternatives will suffice解决此问题的任何提示,或者如果不是最佳替代方案就足够了

I cannot modify the function to take a as an argument, but it needs to be of a specific structure for another package.我不能修改函数以将 a 作为参数,但它需要具有另一个包的特定结构。

That's because the variable a is not defined.那是因为变量a没有定义。 Try with:尝试:

 def func():
   global a 
   a = 1 #for example
   print(a)

The output will be:输出将是:

>>> 1

Maybe you don't have it in that file.也许你在那个文件中没有它。 Have it imported from wherever it was declared and then do the same as you already did.从它声明的任何地方导入它,然后像你已经做的那样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM