Python | TypeError:'NoneType'对象不可调用

时间:2015-01-12 12:07:21

标签: python

我使用以下代码从音频文件中提取一些功能,将它们写入numpy数组文件中:

def calc_ceps(file_list, index, label, method=None, n_mfcc=None, s_rate=None):    
    '''Calculates and saves ceps'''

    if method == None:
        method = 'librosa'
    if s_rate == None:
        s_rate = 22050
    if n_mfcc == None:
        n_mfcc = 20    

    print 'Wave to Ceps:'
    total = len(file_list)    

    for i, file in enumerate(file_list):         

        if method == 'librosa':
            ceps = np.array(librosa.feature.mfcc(*read_wave(file), n_mfcc=n_mfcc))
            ceps.shape = (len(ceps[0]), n_mfcc)

        elif method == 'talkbox':
            ceps = mfcc(read_wave(file)[0])[0]

        write_data('ceps', ceps, label[i], file)
        progress(i, total)  

    progress(199, 199)

函数进度(当前,总计)打印进度, file_popul()提供文件列表, write_data()写入numpy数组到文件。

致电:

>>>get_ceps(*file_popul())() 

虽然 calc_ceps()函数按预期工作100%(即为它获取的所有文件保存numpy数组),但当它结束时(即写入所有文件)我得到以下Traceback :

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

pdb输出步骤:

>>> calc_ceps(*file_popul())()    
Wave to Ceps:
99%> ~/final_project/code/utilities.py(122)calc_ceps()
-> progress(199, 199)
(Pdb) s
--Call--
> ~/python/final_project/code/utilities.py(16)progress()
-> def progress(current, maximum):
(Pdb) s
> ~/python/final_project/code/utilities.py(18)progress()
-> ratio = round((float(current) / float(maximum)), 2) * 100
(Pdb) s
> ~/python/final_project/code/utilities.py(19)progress()
-> if ratio < 100:
(Pdb) s
> ~/python/final_project/code/utilities.py(22)progress()
-> elif ratio == 100:
(Pdb) s
> ~/python/final_project/code/utilities.py(23)progress()
-> sys.stdout.write("\r100%  done! \n")
(Pdb) s
100%  done! 
--Return--
> ~/python/final_project/code/utilities.py(23)progress()->None
->sys.stdout.write("\r100%  done! \n")
(Pdb) s
--Return--
> ~/python/final_project/code/utilities.py(122)calc_ceps()->None
-> progress(199, 199)
(Pdb) s
TypeError: "'NoneType' object is not callable"
> <stdin>(1)<module>()
(Pdb) s
--Return--
> <stdin>(1)<module>()->None
(Pdb) s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
>>> 

使用python 2.7

先谢谢。

编辑:注释掉progress()用法,这是我在pdb中得到的:

>>> calc_ceps(*file_enum())()
Wave to Ceps:
--Return--
> /home/mpir/python/final_project/code/utilities.py(121)calc_ceps()->None
-> import pdb; pdb.set_trace()
(Pdb) s
TypeError: "'NoneType' object is not callable"
> <stdin>(1)<module>()
(Pdb) s
--Return--
> <stdin>(1)<module>()->None
(Pdb) s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
>>> 

这不奇怪吗?

1 个答案:

答案 0 :(得分:2)

你的calc_ceps函数没有return语句,因此calc_ceps(*file_popul())会返回None,你会再尝试再使用一对大括号:calc_ceps(*file_popul())()