官术网_书友最值得收藏!

3 Function Definitions

Function definitions are a way to decompose a large problem into smaller problems. Mathematicians have been doing this for centuries. It's a way to package our Python programming into intellectually manageable chunks.

We'll look at a number of function definition techniques in these recipes. This will include ways to handle flexible parameters and ways to organize the parameters based on some higher-level design principles.

We'll also look at the typing module and how we can create more formal annotations for our functions. We will start down the road toward using the mypy project to make more formal assertions about the data types in use.

In this chapter, we'll look at the following recipes:

  • Function parameters and type hints
  • Designing functions with optional parameters
  • Type hints for optional parameters
  • Using super flexible keyword parameters
  • Forcing keyword-only arguments with the * separator
  • Defining position-only parameters with the / separator
  • Writing hints for more complicated types
  • Picking an order for parameters based on partial functions
  • Writing clear documentation strings with RST markup
  • Designing recursive functions around Python's stack limits
  • Writing testable scripts with the script library switch
主站蜘蛛池模板: 阿坝县| 大名县| 无锡市| 偏关县| 天等县| 沽源县| 陈巴尔虎旗| 凉城县| 新民市| 大港区| 谢通门县| 岳普湖县| 治多县| 宝坻区| 景洪市| 华阴市| 铅山县| 内黄县| 汉中市| 揭西县| 镇平县| 英德市| 连云港市| 资源县| 恭城| 土默特左旗| 鹤岗市| 汝阳县| 灵川县| 星子县| 于田县| 溧阳市| 紫金县| 涟源市| 台中县| 应城市| 梁河县| 宿州市| 绍兴市| 资兴市| 织金县|