How to use the pass statement?

前端 未结 15 2517
旧时难觅i
旧时难觅i 2020-11-22 15:03

I am in the process of learning Python and I have reached the section about the pass statement. The guide I\'m using defines it as being a Null sta

15条回答
  •  暖寄归人
    2020-11-22 15:27

    If you want to import a module, if it exists, and ignore importing it, if that module does not exists, you can use the below code:

    try:
        import a_module
    except ImportError:
        pass
    #rest of your code
    

    If you avoid writing the pass statement and continue writing the rest of your code, a IndentationError would be raised, since the lines after opening the except block are not indented

提交回复
热议问题