“Boilerplate” code in Python?

后端 未结 6 759
终归单人心
终归单人心 2021-02-01 03:07

Google has a Python tutorial, and they describe boilerplate code as \"unfortunate\" and provide this example:

#!/usr/bin/python

# import modules used here -- sy         


        
6条回答
  •  春和景丽
    2021-02-01 03:24

    1. It is repetitive in the sense that it's repeated for each script that you might execute from the command line.
    2. If you put your main code in a function like this, you can import the module without executing it. This is sometimes useful. It also keeps things organized a bit more.
    3. Same as #2 as far as I can tell
    4. Python is generally pretty good at avoiding boilerplate. It's flexible enough that in most situations you can write code to produce the boilerplate rather then writing boilerplate code.

    Off topic question:

    If you don't write code to check the arguments, they are ignored.

提交回复
热议问题