使用 Python 在另一个函数中使用一个函数的变量
Use a variable from one function within another function with 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()
但是当我尝试这个时,我收到一条错误消息,告诉我 best 尚未声明。我不想将 best 声明为全局变量。有没有办法在另一个函数中使用一个函数中的变量?
将一个大函数拆分成多个小函数是一个好习惯,但更紧迫的问题是您应该使用主循环并使您的函数 return 而不是像那样将它们链接起来。
现在下载() -> another_download() -> 下载() -> another_download() -> 下载() -> ...,所以如果用户想要下载 n 个视频,您将有 n * 2 - 1 个函数,直到最后一个完成。
顺便 return 解决了你的问题:
def url():
...
return best
def download():
best = url()
...
这里有几个选项供您选择。我会尽力把它们排好。
选项 1:假设您首先调用 download()
,您可以获得 url()
return 您需要的内容并将其存储在 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()
我认为这些解决方案中的任何一个都能满足您的需求,但在这种特定情况下我会推荐第一个,因为它看起来不像是一个复杂的程序。
我有一个下载视频文件的程序这里是完整的,不要担心它是一个简短的程序。
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()
但是当我尝试这个时,我收到一条错误消息,告诉我 best 尚未声明。我不想将 best 声明为全局变量。有没有办法在另一个函数中使用一个函数中的变量?
将一个大函数拆分成多个小函数是一个好习惯,但更紧迫的问题是您应该使用主循环并使您的函数 return 而不是像那样将它们链接起来。
现在下载() -> another_download() -> 下载() -> another_download() -> 下载() -> ...,所以如果用户想要下载 n 个视频,您将有 n * 2 - 1 个函数,直到最后一个完成。
顺便 return 解决了你的问题:
def url():
...
return best
def download():
best = url()
...
这里有几个选项供您选择。我会尽力把它们排好。
选项 1:假设您首先调用 download()
,您可以获得 url()
return 您需要的内容并将其存储在 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()
我认为这些解决方案中的任何一个都能满足您的需求,但在这种特定情况下我会推荐第一个,因为它看起来不像是一个复杂的程序。