了解函数/变量在类内部的工作方式

问题描述 投票:1回答:1

我正在研究一个Python项目,当用户输入视频名称时,该项目会下载YouTube视频。我从未与班级合作,这是我的第一次,实际上我是从Python书中学到的关于类,函数以及所有内容的信息,但是当我尝试在实际项目中使用它时,我很困惑。这是我的代码:

import pytube
import urllib.request
import urllib.parse
import re

class YouTubeDownloader:
    def __init__(self):
        self.user_music = self.userInput()

    # Ask user what video they want to download
    def userInput(self):
        print("Please type such as: Chopin Nocturne No 9 op 5")
        subject = input("What video do you want to download in YouTube?")
        return subject

    # Search the youtube video with what user type
    def searchYoutube(self, subject):
        query_string = urllib.parse.urlencode({"search_query": subject})
        html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
        search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
        finalURL = "http://www.youtube.com/watch?v=" + search_results[0]
        return finalURL

    # Download the first video
    def downloadVideo(self, finalURL):
        youtube = pytube.YouTube(finalURL)
        video = youtube.streams.first()
        video.download('C:/Users/Muffin/Downloads')


if __name__ == '__main__':
    user = YouTubeDownloader()

所以在这里:

  1. 我是否需要self.user_music,如果需要,为什么需要它?除了self之外,我没有将任何东西传递给__init__,是否有必要?
  2. 我想使用函数subject中的函数def userInput中的def searchYoutube变量,变量会自动传递给类中的不同函数吗?
python class
1个回答
1
投票
  1. 您的代码似乎从未使用过self.user_music,因此,根据您提供的代码段,您无需定义它。如果您没有向__init__传递任何内容,并且在对象实例化期间不需要进行任何自定义初始化,则无需定义构造函数(__init__),因为Python会为您提供默认的构造函数。] >
  2. 在函数内部定义的名称属于该函数的本地范围,并且在该范围之外不可见。如果要使用subject的值,则需要将其作为参数传递给可能要使用它的所有其他函数或方法。您可以将其转换为实例属性:self.subject = 'whatever',并在需要时使用它;在这种情况下,您不需要传递它。
  3. 请注意,答案基于当前代码段。我们看不到您如何使用searchYoutubedownloadVideo,因此最终答案可能有所不同。

编辑:根据注释中的请求更新代码,以显示如何创建实例变量subject

import pytube
import urllib.request
import urllib.parse
import re

class YouTubeDownloader:
    def __init__(self):
        self.subject = self.userInput()

    # Ask user what video they want to download
    def userInput(self):
        print("Please type such as: Chopin Nocturne No 9 op 5")
        subject = input("What video do you want to download in YouTube?")
        return subject

    # Search the youtube video with what user type
    def searchYoutube(self):
        query_string = urllib.parse.urlencode({"search_query": self.subject})
        html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
        search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
        finalURL = "http://www.youtube.com/watch?v=" + search_results[0]
        return finalURL

    # Download the first video
    def downloadVideo(self, finalURL):
        youtube = pytube.YouTube(finalURL)
        video = youtube.streams.first()
        video.download('C:/Users/Muffin/Downloads')


if __name__ == '__main__':
    youtube_downloader = YouTubeDownloader()

    # Example of searching YouTube
    final_url = youtube_downloader.searchYoutube()
© www.soinside.com 2019 - 2024. All rights reserved.