使用Python中的另一个函数中的一个函数的变量
问题描述:
我有一个程序下载视频文件在这里它是完整的,不用担心它的短程序。使用Python中的另一个函数中的一个函数的变量
import pafy
def download():
url = raw_input('Please enter the path to the video\n')
video = pafy.new(url)
vid_title = video.title
best = video.getbest()
streams = video.streams
print(vid_title)
for stream in streams:
print(stream)
print'Resolution: ',best.resolution,'\nExtension : ', best.extension
user_choice = raw_input("Do you want to download this video\ny or n\n")
if user_choice == 'y':
print'Your video will downloaded soon'
filename = best.download(filepath = '/home/mark/new_projects')
another_download()
elif user_choice =='n':
print'You have chosen not to download a video'
another_download()
def another_download():
another_choice = raw_input('Would you like to download another video\ny or n\n')
if another_choice == 'y':
download()
else:
print'Thank for using my program'
download()
我想把它分解成更小的函数。我试图做到这一点:
def url():
url = raw_input('Please enter the path to the video\n')
video = pafy.new(url)
vid_title = video.title
best = video.getbest()
streams = video.streams
print(vid_title)
for stream in streams:
print(stream)
print'Resolution: ',best.resolution,'\nExtension : ', best.extension
def download():
user_choice = raw_input("Do you want to download this video\ny or n\n")
if user_choice == 'y':
print'Your video will downloaded soon'
filename = best.download(filepath = '/home/mark/new_projects')
another_download()
elif user_choice =='n':
print'You have chosen not to download a video'
another_download()
但是,当我尝试这个,我得到一个错误告诉我,最好还没有宣布。我不想宣布最好的全局变量。有没有一种方法使用另一个函数内的一个函数的变量?
答
有你在这里几个选项。我会尽力把他们放在最好的位置。
选项1:假设你在呼唤download()
首先,你可以有url()
回报,你需要什么,并存储在一个变量中download()
方法:
def url():
url = raw_input('Please enter the path to the video\n')
video = pafy.new(url)
vid_title = video.title
best = video.getbest()
streams = video.streams
print(vid_title)
for stream in streams:
print(stream)
print'Resolution: ',best.resolution,'\nExtension : ', best.extension
return best
def download():
user_choice = raw_input("Do you want to download this video\ny or n\n")
if user_choice == 'y':
best = url()
print'Your video will downloaded soon'
filename = best.download(filepath = '/home/mark/new_projects')
another_download()
elif user_choice =='n':
print'You have chosen not to download a video'
another_download()
选项2:您可以使用全局变量,虽然我不知道在这种情况下,使用它们的后果:
best = None
def url():
global best
url = raw_input('Please enter the path to the video\n')
video = pafy.new(url)
vid_title = video.title
best = video.getbest()
streams = video.streams
print(vid_title)
for stream in streams:
print(stream)
print'Resolution: ',best.resolution,'\nExtension : ', best.extension
def download():
global best
user_choice = raw_input("Do you want to download this video\ny or n\n")
if user_choice == 'y':
print'Your video will downloaded soon'
filename = best.download(filepath = '/home/mark/new_projects')
another_download()
elif user_choice =='n':
print'You have chosen not to download a video'
another_download()
我认为这两种解决方案会给你想要的东西,但我会建议先在这种特殊情况下,因为它不SE他们像一个复杂的程序。
答
将一个大功能分解为小功能是一种好习惯,但更迫切的问题是您应该使用主循环并使您的函数返回而不是像这样链接它们。 () - > another_download() - >下载() - > another_download() - >下载() - > ...,所以如果用户想下载n vids,你将有n * 2 - 1功能挂起,直到最后一个完成。
顺便回报解决您的问题:
def url():
...
return best
def download():
best = url()
...
你能显示你的方法解析顺序吗?我的意思是,从何处调用'download()'以及何时,如何调用'url()'。在第二个例子中也要提供一个完整的程序。 – light2yellow
是的,有一种方法可以从另一个函数内的一个函数中使用一个变量。使用*参数*。 – light2yellow
@ light2yellow谢谢,我确实尝试使用一个参数,但是我没有正确地做到这一点,所以我来到这里。我调用了url()并在文件末尾下载 –