“Boilerplate” code in Python?

后端 未结 6 862
终归单人心
终归单人心 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:22

    Let’s take a moment to see what happened when you called import sys:

    • Python looks at a list and brings in the sys module
    • It finds the argv function and runs it

    So, what’s happening here?

    A function written elsewhere is being used to perform certain operations within the scope of the current program. Programming in this fashion has a lots of benefits. It separates the logic from actual labour.

    Now, as far as the boilerplate is concerned, there are two parts:

    • the program itself (the logic), defined under main, and
    • the call part that checks if main exists

    You essentially write your program under main, using all the functions you defined just before defining main (or elsewhere), and let Python look for main.

提交回复
热议问题