我想将函数中定义的变量导入到另一个Python文件
e1.py
def abc():
global a
a=10
e2.py
import e1
def defi():
c=e1.abc.a
print(c)
defi()
我已经搜索过,但没有得到正确的答案
以下是错误 -
Traceback (most recent call last):
File "C:\Users\gkaur\Documents\MSO Editor Tool\e2.py", line 1, in <module>
from e1 import abc
File "C:\Users\gkaur\Documents\MSO Editor Tool\e1.py", line 2
global a
SyntaxError: name 'a' is parameter and global
最佳答案
您没有正确使用文件 1 中的函数,它应该是:
def abc():
a = 10
return a
您应该返回该值。对于第二个文件,它应该是:
import e1
def defi():
c = e1.abc()
print(c)
defi()
如果e1.abc()
末尾没有()
,它实际上并没有告诉函数执行其特定任务。
关于python - 将函数变量从一个 python 文件导入到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33902815/