简体   繁体   English

函数之间的传递列表

[英]Passing list Between Functions

I have the code below where I change the globals. 我在下面的代码中更改了全局变量。 Most of what I have read has said to stay away from globals... In the example below I want to use "make_lists" then print it in "print_list" then clear the list. 我读过的大多数内容都说要远离全局变量...在下面的示例中,我想使用“ make_lists”,然后在“ print_list”中打印,然后清除列表。 I use globals to do this, suggestions on how to avoid globals? 我使用全局变量来做到这一点,关于如何避免全局变量的建议?

I know I could add this all in one function, but this is all part of a bigger function this is the only part I am having problems with. 我知道我可以在一个函数中添加所有这些,但这是一个更大的函数的全部,这是我唯一遇到的问题。 Thank in advance. 预先感谢。

x_lst = []

def make_lists():
    global x_lst

    x_lst.append("Hello")

def print_list():
    global x_lst

    for i in x_lst:
        print(i)

    x_lst = []

def main():
    make_lists()
    print_list()

if __name__ =="__main__":
    main()

To avoid using global, you have to use the return keyword in the function declaration to return a value, which will be your newly created list in our case. 为了避免使用global,您必须在函数声明中使用return关键字来返回一个值,在本例中,该值将是您新创建的列表。 You also have to use arguments in the function declaration, which are the placeholders of the values in the function. 您还必须在函数声明中使用arguments ,这些arguments是函数中值的占位符。 So the reason why you don't need the global value is because you are passing the list from one function to another. 因此,您不需要全局值的原因是因为您要将列表从一个函数传递给另一个函数。

def make_lists():
    return ['Hello']  # Your are creating the list with a single value,
                      # and return it

def print_list(ls):
    for i in ls:      # You are iterating thru the list,
        print(i)      # which is the input of this function

def main():
    print_list(make_lists())  # In your main function you call the make_list()
                              # which will return the newly created list, and
                              # pass it to print_list, which will iterate thru
                              # the list and prints your values out

if __name__ =="__main__":
    main()
def make_list():
    return ['Hello']

def print_list(l):
    for x in l:
        print(x)

def main():
    print_list(make_list())

Pass data into functions with arguments, and pass data out with return values. 通过参数将数据传递到函数中,并通过返回值将数据传递出去。

Here's my simple suggestion, why not give the functions inputs. 这是我的简单建议,为什么不提供函数输入。 Passing in a list to print out instead of using a global variable is an easy alternative. 传递列表以打印出来而不是使用全局变量是一种简单的选择。 If you're looking for a way to keep state for a particular scope, maybe try putting these functions in a class (in other words use OOP) 如果您正在寻找一种为特定范围保留状态的方法,则可以尝试将这些函数放在一个类中(换句话说,使用OOP)

The problem with global variables is that when you declare 'x_lst' as global, it opens up "x_lst" to outside the function, which may not or be your intended goal. 全局变量的问题在于,当您将“ x_lst”声明为全局变量时,它将向函数外部打开“ x_lst”,这可能不是您的目标,也可能不是您的目标。

I've made the minimal set of changes to your code that illustrate how to pass your list instead of using a global. 我对您的代码进行了最少的更改,以说明如何通过列表而不是使用全局列表。 Hope this helps clarify. 希望这有助于澄清。

def make_lists(x_lst):
    x_lst.append("Hello")

def print_list(x_lst):
    for i in x_lst:
        print(i)

def main():
    x_lst = []

    make_lists(x_lst)
    print_list(x_lst)

if __name__ =="__main__":
    main()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM