简体   繁体   English

Python如何将列表返回给其他函数?

[英]Python how to return a list to other functions?

I have been trying to return the list so other functions can access it.我一直在尝试返回列表,以便其他函数可以访问它。 But in all the other functions the variables become undefined.但是在所有其他函数中,变量变得未定义。 The command should be "return twitchClipLinks" right?命令应该是“返回 twitchClipLinks”吧?

def api():
  #API via twitch to get the top clips of Just Chatting
  API_ENDPOINT = 'https://api.twitch.tv/kraken/clips/top?game=Just%20Chatting&period=day&trending=false&limit=6'
  ID = 'REMOVED'
  auth = 'application/vnd.twitchtv.v5+json'

  head = {
    'Client-ID' : ID,
    'Accept' : auth
  }

  r = requests.get(url = API_ENDPOINT, headers = head)

  twitchClipLinks = []
  data = r.json()

  for link in data['clips']:
    store = str(link['url'])
    twitchClipLinks.append(store)
  return twitchClipLinks

在每个函数中,您必须添加如下内容: twitchClipLinks = api()或者您可以将twitchClipLinks定义为global variable并从api()函数中删除return语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python如何从函数返回列表? 传递的列表是不可变的? - How does python return list from functions? List passed is immutable? Python 3函数 - 列出其他函数的值 - Python 3 Functions - List Values for Other Functions 如何返回列表列表中不在 Python 中其他列表中的所有项目? - How to return all item in list of lists that are not in other list of lists in Python? 如何匹配列表的一部分并在python中返回其他部分 - How to match part of a list and return other parts in python Python的tkinter按钮-如何从其他按钮获取函数的返回值? - Python's tkinter Buttons - How to get return values of functions from other buttons? 如何在python中返回列表? - How to return a list in python? 从 Python 中的“异步”函数中的“产量”获取返回值的其他方法? - Other ways to get the return values from "yield" in "async" functions in Python? 如何在 python 中包装列表函数? - How to wrap list functions in python? 如何仅使用 car、cdr、cons 和其他函数拆分列表 (python) - How to split a list only using car, cdr, cons and other functions (python) function 看起来如何,可以将另一个 function 添加到在 python 中并行运行的函数列表中? - How could a function look like, that can add an other function to a list of functions that run parallel in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM