简体   繁体   English

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

[英]Use a variable from one function within another function with Python

I have a program that downloads video files Here it is in full, don't worry its a short program. 我有一个下载视频文件的程序。这里有完整的程序,不用担心它的程序简短。

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

I would like to break it down into smaller functions. 我想将其分解为较小的功能。 I have tried to do this: 我试图做到这一点:

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

But when I try this I get an error telling me that best has not been declared. 但是,当我尝试此操作时,我收到一条错误消息,告诉我尚未宣布最佳状态。 I don't want to declare best as a global variable. 我不想宣布最佳作为一个全局变量。 Is there a way of using a variable from one function inside another function? 有没有一种方法可以在另一个函数中使用一个函数中的变量?

Split a big function into smaller ones is a good habit but a more urgent problem is that you should use a main loop and make your functions return instead of chaining them like that. 将大函数拆分为较小的函数是一个好习惯,但是更紧迫的问题是,应该使用主循环并让函数返回,而不是像这样将它们链接起来。

Right now download() -> another_download() -> download() -> another_download() -> download() -> ..., so if the user wants to download n vids you'll have n * 2 - 1 functions hanging around until the last one finish. 现在download()-> another_download()-> download()-> another_download()-> download()-> ...,因此,如果用户要下载n个vid,您将拥有n * 2-1个功能一直到最后一个完成。

Incidentally return solves your problem : 顺便返回解决您的问题:

def url():
    ...
    return best 

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

There are a couple options for you here. 这里有几个选项供您选择。 I will try and lay them out best I can. 我会尽力将它们布置的最好。

Option 1: Assuming you are calling download() first, you can have url() return what you need and store that in a variable in the download() method: 选项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()

Option 2: You could use global variables, though I don't know the ramifications of using them in this case: 选项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()

I think either of these solutions will give you what you want, but I would recommend the first in this specific case as it doesn't seem like a complex program. 我认为这两种解决方案都可以为您提供所需的解决方案,但是在这种特定情况下,我建议您使用第一种解决方案,因为它似乎并不复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 function 的变量用于另一个变量? (Python) - How to use variable of function for another one? (Python) Python在另一个函数中使用一个函数的变量 - Python Using variable from one function in another 无法在tkinter python中将一个函数的变量用于另一个函数 - unable to use variable of one function to another function in tkinter python 如何在python中从另一个函数中调用一个函数 - How to call one function from within another function in python 在另一个 function (Python)中使用来自一个 function 的变量的最佳方法是什么? - What is the best way to use a variable from one function in another function (Python)? 使用python会话将变量从一个函数传递给另一函数 - Using python sessions to pass variable from one function to another function 在Python中调用一个函数的变量到另一个函数 - Calling a variable from one function to another function in Python 如何将变量从一个 function 传递到 python 中的另一个 function - How to pass a variable from one function to another function in python 如何在另一个 function 中使用前一个 function 返回的变量? (Python) - How to use a returned variable from a previous function in another function? (python) 如何使用一个函数变量转换为另一个函数? - how to use one function variable into another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM