Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
in file1.py def foo(): sum=2+4 return sum def bar(sum): print sum in file2.py import file1 file1.foo() file1.bar(sum)
当我这样做时,我会收到这样的错误
NameError: name 'sum' is not defined
如何调用函数的返回值...帮帮我!!
将返回的值存储file1.foo在变量中。在函数内部创建的变量仅对该函数是本地的,不能在该函数外部访问。
file1.foo
import file1 ret = file1.foo() #strore it's return value in `ret` file1.bar(ret) #now pass `ret` to this function
并且不要sum用作变量名,因为它会掩盖内置函数sum。
sum