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

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
主站蜘蛛池模板: 平山县| 宁波市| 都兰县| 赫章县| 灌云县| 防城港市| 长沙市| 尉犁县| 满城县| 静乐县| 乌拉特中旗| 务川| 留坝县| 大宁县| 临城县| 什邡市| 赤峰市| 施甸县| 霍山县| 绍兴县| 抚松县| 新巴尔虎右旗| 吉隆县| 淄博市| 桓仁| 井冈山市| 枣庄市| 越西县| 故城县| 灵台县| 秦安县| 博客| 镇原县| 张家川| 潞城市| 宝丰县| 西城区| 安乡县| 白城市| 合江县| 宣化县|