gpt4 book ai didi

python - 在一个函数中创建列表,然后在 python (pythonista ios) 中的另一个函数中使用它

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:49 27 4
gpt4 key购买 nike

非常感谢一些帮助。具体来说,我在 pythonista 中使用 UI 模块以及一个 .pyui 文件。

我的应用程序有两个按钮 - 一个称为“编译”,另一个称为“执行”。

编译:

# First Button
def compile(sender):
*creates a list by calling another function*
*let's say this is called "myList"*

执行:

# Second button that pulls a random element from the list
def execute(sender):
random.choice(myList)

但是,当我在按钮 1 中创建 myList 时(它工作正常),我无法在按钮 2 中调用它,因为它们是两个独立的实例。

因此,有没有办法以某种方式使列表“全局”化?这里最好和最 pythonic 的解决方案是什么?

最佳答案

只需在函数外声明列表

my_list = []

def fun1():
*do smth with my_list*

def fun2():
*do smth with my_list too*

关于python - 在一个函数中创建列表,然后在 python (pythonista ios) 中的另一个函数中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44197830/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com