【问题标题】:Why does my object keep turning in to a NoneType?为什么我的对象总是变成 NoneType?
【发布时间】:2021-07-11 18:00:08
【问题描述】:

我正在构建一个 Youtube 音频采样程序,并设置了一个名为 time_snip 的对象。 包含样本开始和结束的对象,以毫秒为单位。 我注意到,如果我输入正确的时间,我会得到预期的输出,但是如果我以错误的格式输入时间(尝试/例外设置),并且我以正确的方式再次输入,我会得到一个 @987654321 @

time_snip 类:

    class time_snip:
        def __init__(self,start_hour,start_min,start_sec,start_msec,end_hour,end_min,end_sec,end_msec):
            self.start = (start_hour*60*60*1000)+(start_min * 60 * 1000) + (start_sec * 1000)+(start_msec) # Time to milliseconds
            self.end = (end_hour*60*60*1000)+(end_min * 60 * 1000) + (end_sec * 1000)+(end_msec) # Time to milliseconds
            self.start_ind = (str(start_min)+':'+str(start_sec))
            self.end_ind =(str(end_min)+':'+str(end_sec))

主要代码:

    from pytube import YouTube
    from TimeSnip import time_snip
    time_options = input("Would you like to find by time(1) or by word(2)?: ")
    url ="https://www.youtube.com/watch?v=vGwNPeEXEDM&ab_channel=SilasLaspada"
    ytd = YouTube(URL)
    def time_set(youtube_object):
            try:
                hr_length = int(ytd.length/3600)
                min_length = int((ytd.length%3600)/60)
                sec_length = ytd.length%60

                print("the video is " + str(hr_length)+':'+str(min_length)+':'+str(sec_length) +'long')
                start_time_srt_format = input("Enter start time in 'hr:min:sec,msec' format: ")
                end_time_srt_format = input("Enter end time in 'hr:min:sec,msec' format: ")
                start_parts = start_time_srt_format.split(':')
                end_parts = end_time_srt_format.split(':')
                start_secmsec = start_parts[2].split(',')
                end_secmsec = end_parts[2].split(',')
                start_hr = int(start_parts[0])
                start_min = int(start_parts[1])
                start_sec = int(start_secmsec[0])
                start_msec = int(start_secmsec[1])
                end_hr = int(end_parts[0])
                end_min = int(end_parts[1])
                end_sec = int(end_secmsec[0])
                end_msec = int(end_secmsec[1])

                word = (str(start_sec)+'-'+str(end_sec))
                timeSnip = time_snip(start_hr, start_min, start_sec, start_msec, end_hr, end_min,end_sec,end_msec)
                return timeSnip
            except IndexError:
                print("Invalid input! Try again!")
                time_set(youtube_object)
    if time_options =='1':
        timeSnip = time_set(ytd)
        word = str(timeSnip.start/1000)+' sec'
    elif time_options =='2':
        word = input("Enter word: ")

我不知道为什么会这样。

【问题讨论】:

    标签: python object reset nonetype


    【解决方案1】:

    在 except 子句中,您不返回任何内容。因此,如果异常触发,您会再次调用该函数(这不是正确的方法),但即使成功并返回一个值,您也会将其丢弃。将该行更改为 return time_set(youtube_object),但最好将其设为 while 循环而不是递归调用。

    【讨论】:

    • 使用while循环是正确。一个函数调用自己只是为了“回到开头”是完全错误的。
    • 谢谢蒂姆!不过我想知道,为什么我不应该使用recurtion??
    • 递归有它的位置,就像在数学编码或树搜索中一样,但它有开销。如果用户无法获得正确的格式,您可能会在此处深入许多级别。从设计的角度来看,这应该是一个“循环直到数据有效”的模式。
    • 一般来说,最好避免递归。每次递归都会在调用栈中增加一层。
    猜你喜欢
    • 2014-02-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2020-11-07
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多