2016-08-18 7 views
0

Ich habe ein Programm, das Videodateien herunterlädt Hier ist es in vollem Umfang, keine Sorge, es ist ein kurzes Programm.Verwenden Sie eine Variable aus einer Funktion innerhalb einer anderen Funktion mit 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() 

Ich möchte es in kleinere Funktionen aufteilen. Ich habe versucht, dies zu tun:

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

Aber wenn ich versuche, diese bekomme ich einen Fehler mir zu sagen, dass am besten nicht erklärt. Ich möchte nicht als globale Variable am besten deklarieren. Gibt es eine Möglichkeit, eine Variable aus einer Funktion in einer anderen Funktion zu verwenden?

+0

Können Sie die Reihenfolge Ihrer Methodenauflösung anzeigen? Ich meine, von wo 'download()' aufgerufen wird und wann dasselbe über 'url()'. Stellen Sie auch im zweiten Beispiel ein vollständiges Programm bereit. – light2yellow

+1

Und ja, es gibt eine Möglichkeit, eine Variable aus einer Funktion in einer anderen Funktion zu verwenden. Verwenden Sie einen * Parameter *. – light2yellow

+0

@ light2yellow Danke, ich habe versucht, einen Parameter zu verwenden, aber ich habe es nicht richtig gemacht, deshalb bin ich hierher gekommen. Ich rief beide URL() und downloaden Sie am Ende der Datei –

Antwort

0

Es gibt ein paar Möglichkeiten für Sie hier. Ich werde versuchen, sie so gut wie möglich zu verteilen.

Option 1: Angenommen, Sie rufen download() zuerst, Sie url() Rückkehr haben können, was Sie brauchen, und speichern, die in einer Variablen in der download() Methode:

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: Sie können globale Variablen verwenden, obwohl ich weiß nicht, die Auswirkungen von ihnen in diesem Fall mit:

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

denke ich, eine dieser Lösungen, die Sie geben, was Sie wollen, aber ich würde die erste in diesem speziellen Fall empfehlen, da es nicht se em wie ein komplexes Programm.

0

Eine große Funktion in kleinere aufteilen ist eine gute Angewohnheit, aber ein dringenderes Problem ist, dass Sie eine Hauptschleife verwenden und Ihre Funktionen zurückkehren sollten, anstatt sie so zu verketten.

Jetzt herunterladen() -> another_download() -> download() -> another_download() -> download() -> ..., wenn der nutzer also n vids herunterladen will, hast du n * 2 - 1 Funktionen hängen bis zum letzten Ziel.

übrigens Rückkehr löst Ihr Problem:

def url(): 
    ... 
    return best 

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