【问题标题】:Create list in one function, then use it in another in python (pythonista ios)在一个函数中创建列表,然后在 python 中的另一个函数中使用它(pythonista ios)
【发布时间】:2017-10-27 03:02:21
【问题描述】:

非常感谢您的帮助。具体来说,我在 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 的解决方案是什么?

【问题讨论】:

    标签: python ios list user-interface pythonista


    【解决方案1】:

    只需在函数之外声明列表

    my_list = []
    
    def fun1():
        *do smth with my_list*
    
    def fun2():
        *do smth with my_list too*
    

    【讨论】:

    • 嗯,这很简单。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    相关资源
    最近更新 更多