我有以下代码

    if self.download_format == 'mp3':
        raise NotImplementedError
    elif self.download_format == 'wav':
        with NamedTemporaryFile(suffix='.wav') as wavfile:
            self.download_wav_recording(call, wavfile.name)
            convert_wav_to_mp3(wavfile.name, filename)

并且pylint报这个错误
R1720: Unnecessary "elif" after "raise" (no-else-raise)

这个错误的动机是什么?为什么这段代码不行?

最佳答案

    if self.download_format == 'mp3':
        raise NotImplementedError
    elif self.download_format == 'wav':
        with NamedTemporaryFile(suffix='.wav') as wavfile:
            self.download_wav_recording(call, wavfile.name)
            convert_wav_to_mp3(wavfile.name, filename)

这相当于
    if self.download_format == 'mp3':
        raise NotImplementedError
    if self.download_format == 'wav':
      with NamedTemporaryFile(suffix='.wav') as wavfile:
          self.download_wav_recording(call, wavfile.name)
          convert_wav_to_mp3(wavfile.name, filename)

因此来自pylint的警告
raise 会导致控制流中断 - 所以你不需要使用 elif 而是可以使用 if

关于python - pylint R1720 : Unnecessary "elif" after "raise" (no-else-raise),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55632832/

10-12 23:09