使用不同函数中的字符串名称

Res*_*eid 1 python string function

我需要使用movies_list第二个中的第一个函数.我怎么做?

def movie():
    movies_list = [movie.strip() for movie in movies_list]
    movie_explorer()

def rand():
    rand_item = print(random.choice(movies_list))
Run Code Online (Sandbox Code Playgroud)

Bha*_*Rao 6

好的

使用return和参数

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))
Run Code Online (Sandbox Code Playgroud)

并且在呼叫时rand记得将该功能称为

rand(movie())
Run Code Online (Sandbox Code Playgroud)

添加一行

global movies_list
Run Code Online (Sandbox Code Playgroud)

作为两个功能的第一行

丑陋的

您可以使用globals可用的对象.(在此添加以完成押韵)

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)
Run Code Online (Sandbox Code Playgroud)