在不同的功能中使用字符串的名称 [英] Using name of strings in different functions
本文介绍了在不同的功能中使用字符串的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从第二个函数的第一个函数中使用 movies_list
。
def movie():
movies_list = [movie.strip()for movie in movies_list]
movie_explorer()
$ b $ def rand():
rand_item = print(random.choice(movies_list))
解决方案
好
使用返回
和参数
def movie ():
movies_list = [movie.strip()用于movies_list中的电影)
movie_explorer()
返回movies_list
$ b def rand(movies_list):
rand_item = print(random.choice(movies_list))
当调用 rand
请记得调用函数作为
rand(movie())
坏
添加一行
global movies_list
作为这两个函数的第一行
和丑
您可以使用 globals
对象可用。 (在这里添加它来完成韵)
def movie():
全局movie_returns
movie_returns = [movie.strip()for movies in movies_list]
movie_explorer()
#不返回
def rand():#无参数
movies_list = next(((如果v =='movies_return'))
rand_item = random.choice(movies_list)
I need to use movies_list
from the first function in the second. How do I do that?
def movie():
movies_list = [movie.strip() for movie in movies_list]
movie_explorer()
def rand():
rand_item = print(random.choice(movies_list))
解决方案
The Good
Use return
and arguments
def movie():
movies_list = [movie.strip() for movie in movies_list]
movie_explorer()
return movies_list
def rand(movies_list):
rand_item = print(random.choice(movies_list))
And when calling rand
remember to call the function as
rand(movie())
The Bad
Add a line
global movies_list
as the first line in both functions
And the Ugly
You can make use of the globals
object available. (Adding it here to complete the rhyme)
def movie():
global movie_returns
movie_returns = [movie.strip() for movie in movies_list]
movie_explorer()
# No return
def rand(): # No argument
movies_list = next((globals()[v] for v in globals() if v=='movies_return'))
rand_item = random.choice(movies_list)
这篇关于在不同的功能中使用字符串的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文