簡體   English   中英

在一個函數中創建列表,然后在python(pythonista ios)中的另一個函數中使用它

[英]Create list in one function, then use it in another in python (pythonista ios)

非常感謝您的幫助。 具體來說,我正在使用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時(它工作正常),但在button2中卻無法調用它,因為它們是兩個單獨的實例。

因此,有沒有辦法以某種方式使列表“全局”? 什么是最好和最pythonic解決方案?

只是在函數外部聲明列表

my_list = []

def fun1():
    *do smth with my_list*

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM