从其他功能打印列表



如何使其工作我应该使用gloable变量还是args?我很困惑。

def getvenueurl():
# code as part of a loop
venueURList.append(tMD)
# end loop

def getraceurl():
print(venueURList)

getvenueurl()
getraceurl()

在这种情况下,您可以在函数范围之外定义变量
示例:

venueURList = []
def getvenueurl():
# code as part of a loop
venueURList.append(tMD)
# end loop

def getraceurl():
print(venueURList)

getvenueurl()
getraceurl()

只需在函数中返回列表。将返回的值存储在一个变量中,并将其传递给一个新函数。

def getvenueurl():
# code as part of a loop
venueURList.append(tMD)
return venueURList
# end loop

def getraceurl(lst):
print(lst)

venueList = getvenueurl()
getraceurl(venueList)

或者另一种选择是使用global venueURList

要访问函数内部的全局变量,必须键入:

global venueURList

在引用或修改(在您的情况下,附加并打印(之前

通常,最好将全局变量传递到参数中,然后返回。

def getvenueurl(venueList):
venueList.append(tMD)
return venueList

最新更新