成功解决NameError: name 'apply' is not defined

解决问题

NameError: name 'apply' is not defined

解决方法

      因为python3和python2之间语言断层的原因。python2有一个叫做apply()的全局函数,它使用一个函数f和一个列表[a,b,c]作为参数,返回值是f(a,b,c).可以直接调用这个函数,在列表前添加一个星号作为参数传递给它来完成同样的事情。在python3里,apply()函数不再存在;必须使用星号标记。


两种方式更改

T1

try:
    apply = apply
except NameError:
def apply(f, *args, **kw):
return f(*args, **kw)

T2

    def run(self): 
        print ('staring', self.name, 'at:', ctime())
        self.res=self.func()   #python2中的用法,apply(self.func, self.args)
        print( self.name, 'finished at:', ctime())
    def run(self):  
        print ('staring', self.name, 'at:', ctime())  
        self.res=self.voice_tts()    ##python2中的用法,apply(self.voice_tts,())的用法
        print( self.name, 'finished at:', ctime())


猜你喜欢

转载自blog.csdn.net/qq_41185868/article/details/80502658