使用Python中另一个函数中的一个函数的变量

时间:2016-08-18 15:35:38

标签: python variables pafy

我有一个下载视频文件的程序在这里它已经完整,不用担心它是一个简短的程序。

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()

但是当我尝试这个时,我得到一个错误,告诉我最好还没有被宣布。我不想声明最好作为全局变量。有没有办法在另一个函数中使用一个函数的变量?

2 个答案:

答案 0 :(得分:0)

将一个大函数拆分成较小的函数是一个好习惯,但更紧急的问题是你应该使用一个主循环并使你的函数返回而不是像这样链接它们。

现在下载() - > another_download() - >下载() - > another_download() - >下载() - > ...,所以如果用户想要下载n个视频,你将有n * 2-1个功能,直到最后一个完成。

顺便提一下,返回解决了您的问题:

def url():
    ...
    return best 

def download():
    best = url()
    ...

答案 1 :(得分:0)

这里有几个选项。我会尽力把它们尽可能地打好。

选项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()

我认为这些解决方案中的任何一个都能满足您的需求,但我会在这个特定情况下推荐第一个,因为它似乎不是一个复杂的程序。