在不使用全局函数的情况下在其他函数中调用变量 [英] Calling a variable in a different function without using global
本文介绍了在不使用全局函数的情况下在其他函数中调用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在另一个函数中定义的函数中使用变量/列表,而不使其成为全局变量.
I'm trying to use a variable / list in a function that is defined in another function without making it global.
这是我的代码:
def hi():
hello = [1,2,3]
print("hello")
def bye(hello):
print(hello)
hi()
bye(hello)
此刻,我得到的错误是"bye(hello)"中的"hello"未定义.
At the moment I am getting the error that "hello" in "bye(hello)" is not defined.
我该如何解决?
推荐答案
如果不想使用全局变量,最好的选择就是从hi()
内部调用bye(hello)
.
if you don't want to use a global variable, your best option is just to call bye(hello)
from within hi()
.
def hi():
hello = [1,2,3]
print("hello")
bye(hello)
def bye(hello):
print(hello)
hi()
这篇关于在不使用全局函数的情况下在其他函数中调用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文