简体   繁体   English

如何在另一个 function 中使用前一个 function 返回的变量? (Python)

[英]How to use a returned variable from a previous function in another function? (python)

I want to use a list that was created from a previous function in my other function. After a bit of research it seems using return is the way of doing it.我想在我的另一个 function 中使用从之前的 function 创建的列表。经过一些研究,似乎使用return是这样做的方式。 However I cannot get it to work.但是我无法让它工作。 This is my code:这是我的代码:

def FunctionA():
  all_comments1 = [1,2,3,4]
  return all_comments1

def FunctionB():
  FunctionA()
  all_comment_string1 = ''.join(all_comments1)
  newlistings1 = all_comment_string1.split('\n')
  print(newlistings1)

def DoSomething():
  FunctionB()

  DoSomething()

It gives me an error:它给了我一个错误:

NameError: name 'all_comments1' is not defined NameError:未定义名称“all_comments1”

I want to know how I can define the variable successfully.我想知道如何成功定义变量。

You have to define a new variable.您必须定义一个新变量。 Right now you call the FunctionA() but don't save its return value.现在您调用 FunctionA() 但不保存其返回值。 To do so, simply make a new variable like so:为此,只需像这样创建一个新变量:

def FunctionA():
    all_comments1 = [1,2,3,4]
    return all_comments1

def FunctionB():
    all_comments = FunctionA()
    print(all_comments)

FunctionB()

>> [1,2,3,4]

I believe you are looking to use global variables between your functions.我相信您希望在函数之间使用全局变量 Modify your code to the following:将您的代码修改为以下内容:

def FunctionA():
    # Declare all_comments1 as a global variable
    global all_comments1
    all_comments1 = [1, 2, 3, 4]
    return all_comments1


def FunctionB():
    # Access global variable
    global all_comments1
    # Run functionA otherwise global variable will not be defined
    FunctionA()

    # Map objects from `all_comments1` to str, since they are int
    all_comment_string1 = ''.join(map(str, all_comments1))
    newlistings1 = all_comment_string1.split('\n')
    print(newlistings1)

def DoSomething():
    FunctionB()

DoSomething()

>> ['1234']

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

相关问题 python 将另一个 function 的返回值设置为在 header 中使用的变量 - python set returned value from another function as a variable to use in header Python:使用从一个函数返回的值到另一个函数 - Python: Use returned values from a function to another function 如何将返回的值(从先前的函数中)读取到pandas,python中? - How to read the returned value (from previous function) into pandas, python? 如何在另一个函数中使用一个函数的两个返回值? 主要() - How to use two returned values from a function in another function? MAIN() Python Nuke-如何使用另一个函数的变量结果 - Python Nuke - How to use variable result from another function 如何使用函数返回的列表,作为 Python 中的函数参数 - How to use returned list from function, as function args in Python 如何将 function 的变量用于另一个变量? (Python) - How to use variable of function for another one? (Python) 如何在python的另一个应用程序中使用函数返回的值? - How can I use a value returned by a function in another application in python? 如何在其他函数中使用返回的变量。 Python - How can I use returned variable in other function. Python 使用Python在另一个函数中使用一个函数中的变量 - Use a variable from one function within another function with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM