- Modern Python Cookbook
- Steven F. Lott
- 189字
- 2021-06-11 18:49:13
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
推薦閱讀
- Linux運維之道(第3版)
- SOA實踐者說
- Windows Phone 7.5 Data Cookbook
- Linux性能優化
- Kubernetes從入門到實踐
- 嵌入式實時操作系統μC/OS原理與實踐
- 8051軟核處理器設計實戰
- Linux使用和管理指南:從云原生到可觀測性
- Ceph分布式存儲實戰
- Windows 10從新手到高手
- Linux網絡操作系統項目教程(RHEL 7.4/CentOS 7.4)(第3版)(微課版)
- Drupal 7 Cookbook
- Agile IT Security Implementation Methodology
- 應急指揮信息系統設計
- Microsoft Hyper-V Cluster Design