从函数内部的其他python模块访问python变量

从函数内部的其他python模块访问python变量,python,function,variables,module,Python,Function,Variables,Module,我对python非常陌生,在我的一个项目中遇到了一个问题,无论我读了多少篇文章,我都无法克服 所以我有两个.py文件 第一个:(First.py) 第二个:(Second.py) 因此second.py创建一个全局变量,first.py导入second.py,并尝试打印出我的_var。但由于某些原因,我总是收到以下错误消息: Traceback (most recent call last): File "first.py", line 3, in <module&

我对python非常陌生,在我的一个项目中遇到了一个问题,无论我读了多少篇文章,我都无法克服

所以我有两个.py文件

第一个:(First.py)

第二个:(Second.py)

因此second.py创建一个全局变量,first.py导入second.py,并尝试打印出我的_var。但由于某些原因,我总是收到以下错误消息:

Traceback (most recent call last):
  File "first.py", line 3, in <module>
    print(answer)
AttributeError: module 'second' has no attribute 'answer'
回溯(最近一次呼叫最后一次):
文件“first.py”,第3行,在
打印(答案)
AttributeError:模块“second”没有属性“answer”


我可以请你帮忙吗?我真的很努力,但看在上帝的份上,我想不出来

如果让函数返回以下值会更好:

def功能(一、二):
答案=一加二
回覆
然后您可以像这样设置其他文件:

from second import function
answer = function(10, 30)
print(answer)

此外,函数是一个糟糕的函数名称,请将其称为描述性的

没有全局变量定义。
替换second.py:

answer = None

def function(one, two):
    global answer
    answer = one + two

我相信这个问题的答案是另一种方式,你可以参考。您已经在函数内部定义了
global answer
(不在函数外部,也不为其赋值),因此在
import
时,此变量即使具有全局性质,也尚未声明。如果没有太多关于如何重构解决方案的细节,您只需要以这种方式编写
first.py

import second
second.function(10, 30)
print(second.answer)

通过这种方式,您没有更改second.py,也没有根据需要导入全局变量。

好吧,让我解释一下从一个脚本导入到另一个脚本时会发生什么。当你写的时候:

from second import *
它将名称从
second.py
复制到
first.py
的命名空间中。因此,如果在
first.py
中重新分配
second.py
(在您的案例中是
answer
)的任何变量,则更改将只在
first.py
中进行,而不会在
second.py
中进行。所以,我希望您已经理解,
first.py的
answer
second.py的
answer
都占用了不同的内存位置。为了便于您理解,我编写了一个测试,阅读代码并查看输出,您将了解导入某些内容时发生的情况:

second.py

answer = 0 # you've to declare "answer" as as global variable
           # this is one mistake you've done in your code

def function(one, two):
    global answer
    answer = one + two

def print_answer_in_second():
    print("in second: {}".format(answer))
第一个.py

from second import *

print("in first: {}".format(answer))
print_answer_in_second()

function(10, 30) # calling function() to change answer value in second.py

print("in first: {}".format(answer))
print_answer_in_second()

answer = 100 + 800; # changing answer value in first.py

print("in first: {}".format(answer))
print_answer_in_second()
in first: 0
in second: 0
in first: 0
in second: 40
in first: 900
in second: 40
import glob

def function(a, b):
    glob.answer = a + b

def print_answer_in_second():
    print("In second: {}".format(glob.answer))
import glob

from new_second import function, print_answer_in_second
# note: "import ... from *" is a very bad practice
#       so, avoid use that style of import

function(10, 30)

print("In first: {}".format(glob.answer))
print_answer_in_second()
In first: 40
In second: 40
输出

from second import *

print("in first: {}".format(answer))
print_answer_in_second()

function(10, 30) # calling function() to change answer value in second.py

print("in first: {}".format(answer))
print_answer_in_second()

answer = 100 + 800; # changing answer value in first.py

print("in first: {}".format(answer))
print_answer_in_second()
in first: 0
in second: 0
in first: 0
in second: 40
in first: 900
in second: 40
import glob

def function(a, b):
    glob.answer = a + b

def print_answer_in_second():
    print("In second: {}".format(glob.answer))
import glob

from new_second import function, print_answer_in_second
# note: "import ... from *" is a very bad practice
#       so, avoid use that style of import

function(10, 30)

print("In first: {}".format(glob.answer))
print_answer_in_second()
In first: 40
In second: 40
我希望你能了解进口

现在,我们如何才能完美地解决这个问题?全局变量是语言的重要组成部分,如果没有
全局变量
,编程世界就无法运行。但是
globals
也会产生问题。很难维护
globals
。那么,
globals
的最佳实践是什么呢

好吧,如果你正在开发一个应用程序或任何项目,最好的做法是将你的应用程序或项目的
globals
放在一个文件中,这样我们就可以很容易地找到它们并更改它们的值。让我们调用我们的
全局文件
glob.py
。下面是glob.py的代码:

glob.py:

# declaring global answer
answer = 0
现在,让我们宣布
new\u second.py

新建\u second.py

from second import *

print("in first: {}".format(answer))
print_answer_in_second()

function(10, 30) # calling function() to change answer value in second.py

print("in first: {}".format(answer))
print_answer_in_second()

answer = 100 + 800; # changing answer value in first.py

print("in first: {}".format(answer))
print_answer_in_second()
in first: 0
in second: 0
in first: 0
in second: 40
in first: 900
in second: 40
import glob

def function(a, b):
    glob.answer = a + b

def print_answer_in_second():
    print("In second: {}".format(glob.answer))
import glob

from new_second import function, print_answer_in_second
# note: "import ... from *" is a very bad practice
#       so, avoid use that style of import

function(10, 30)

print("In first: {}".format(glob.answer))
print_answer_in_second()
In first: 40
In second: 40
新的\u first.py

from second import *

print("in first: {}".format(answer))
print_answer_in_second()

function(10, 30) # calling function() to change answer value in second.py

print("in first: {}".format(answer))
print_answer_in_second()

answer = 100 + 800; # changing answer value in first.py

print("in first: {}".format(answer))
print_answer_in_second()
in first: 0
in second: 0
in first: 0
in second: 40
in first: 900
in second: 40
import glob

def function(a, b):
    glob.answer = a + b

def print_answer_in_second():
    print("In second: {}".format(glob.answer))
import glob

from new_second import function, print_answer_in_second
# note: "import ... from *" is a very bad practice
#       so, avoid use that style of import

function(10, 30)

print("In first: {}".format(glob.answer))
print_answer_in_second()
In first: 40
In second: 40
输出

from second import *

print("in first: {}".format(answer))
print_answer_in_second()

function(10, 30) # calling function() to change answer value in second.py

print("in first: {}".format(answer))
print_answer_in_second()

answer = 100 + 800; # changing answer value in first.py

print("in first: {}".format(answer))
print_answer_in_second()
in first: 0
in second: 0
in first: 0
in second: 40
in first: 900
in second: 40
import glob

def function(a, b):
    glob.answer = a + b

def print_answer_in_second():
    print("In second: {}".format(glob.answer))
import glob

from new_second import function, print_answer_in_second
# note: "import ... from *" is a very bad practice
#       so, avoid use that style of import

function(10, 30)

print("In first: {}".format(glob.answer))
print_answer_in_second()
In first: 40
In second: 40

我希望您了解如何正确使用
globals
,而不影响代码的其他部分。

使用
返回
。如果要返回多个答案,请尝试以下操作

first.py:

来自第二个导入函数的

ans1,ans2=函数(1,2,3)
打印(ans1、ans2)#3 5
第二点:

answer = None

def function(one, two):
    global answer
    answer = one + two
def函数(一、二、三):
回答1=一+二
回答2=二加三
回答1,回答2

您可以在
函数
中使用
return
,如果您让函数返回答案而不是生成全局变量,那会更好。您可以在以下内容中找到答案:谢谢,这看起来很有效,但现在我有另一个问题。如果我需要返回多个答案,该怎么办?PS:我已经更改了函数的名称