python - Passing list Between Functions -


i have code below change globals. of have read has said stay away globals... in example below want use "make_lists" print in "print_list" clear list. use globals this, suggestions on how avoid globals?

i know add in 1 function, part of bigger function part having problems with. thank in advance.

x_lst = []  def make_lists():     global x_lst      x_lst.append("hello")  def print_list():     global x_lst      in x_lst:         print(i)      x_lst = []  def main():     make_lists()     print_list()  if __name__ =="__main__":     main() 

to avoid using global, have use return keyword in function declaration return value, newly created list in our case. have use arguments in function declaration, placeholders of values in function. reason why don't need global value because passing list 1 function another.

def make_lists():     return ['hello']  # creating list single value,                       # , return  def print_list(ls):     in ls:      # iterating thru list,         print(i)      # input of function  def main():     print_list(make_lists())  # in main function call make_list()                               # return newly created list, ,                               # pass print_list, iterate thru                               # list , prints values out  if __name__ =="__main__":     main() 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -