SlideShare a Scribd company logo
1 of 25
Download to read offline
Python 类型检查实践
2020.08
⽬录
• Quick Start

• 为什么要引⼊类型检查

• Python 类型检查功能详解

• 进阶知识
Quick Start
Quick Start
Quick Start
⽬录
• Quick Start

• 为什么要引⼊类型检查

• Python 类型检查功能详解

• 进阶知识
Why Go
• 静态类型:

• 编译期即可发现错误,便于维护和重构;

• 使⽤ TS 或 Flow 可以让 Bug 减少 15%;

• 对提升性能也有帮助;

• Killer Apps: gRPC, Kubernetes, Prometheus, InfluxDB;

• ⾕歌背景,适合⼤型项⽬;

• 其它:⼊⻔简单,⼯具完善,便于部署;

• 不会完全替代 Python、Java。
概念梳理
• 静态类型:在编译期进⾏类型检查;

• 动态类型:在运⾏期进⾏类型检查;

• 强类型:不允许隐式类型转换;

• 弱类型:允许隐式类型转换;
静态类型的优点
• 更早发现 Bug;

• 提升代码可读性;

• 优化代码提示;

• 协助代码重构;

• 提升性能;

• 缺点:

• 额外的⼯作量;

• 牺牲灵活性;
静态类型的优点
• 更早发现 Bug;

• 提升代码可读性;

• 优化代码提示;

• 协助代码重构;

• 提升性能;

• 缺点:

• 额外的⼯作量;

• 牺牲灵活性;
静态类型的优点
• 更早发现 Bug;

• 提升代码可读性;

• 优化代码提示;

• 协助代码重构;

• 提升性能;

• 缺点:

• 额外的⼯作量;

• 牺牲灵活性;
其他语⾔⽐较
TypeScript Hack
⽬录
• Quick Start

• 为什么要引⼊类型检查

• Python 类型检查功能详解

• 进阶知识
相关 PEP
• PEP 3107 Function Annotation (Python 3.0)

• PEP 483 The Theory of Type Hints (Python 3.5)

• PEP 484 Type Hints (Python 3.5)

• PEP 526 Syntax for Variable Annotations (Python 3.6)

• PEP 563 Postponed Evaluation of Annotations (Python 3.7)

• PEP 544 Protocols (Python 3.8)

• PEP 589 TypedDict (Python 3.8)

• PEP 591 Adding a final qualifier to typing (Python 3.8)
渐进式类型检查
• 只检查有类型标注的代码;

• 变量类型默认为 Any:

• 任何类型的变量都可以赋值给 Any;

• Any 可以作为任意类型使⽤;

• Any vs. object

• 如果函数签名没有做类型标注,则函数内的本地变量不会做类型推导。
类型标注的三种⽅式
1. 注解 2. 注释
3. 接⼝⽂件 *.pyi
Typeshed
• github.com/python/typeshed

• stdlib

• third_party

• flask

• requests

• dateutil
常⽤类型
类
⽬录
• Quick Start

• 为什么要引⼊类型检查

• Python 类型检查功能详解

• 进阶知识
Generics
Duck Typing
• collections.abc

• Container: __contains__

• Sized: __len__

• Iterable: __iter__

• Collection: __contains__, __len__, __iter__

• Sequence: Collection, __getitem__, index, count

• MutableSequence: Sequence, __setitem__

• Mapping: Collection, __getitem__, get, items

• typing

• Sequence[int]

• List[int]
Duck Typing
Runtime Validation
谢谢

More Related Content

Similar to Python 类型检查实践

⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨Wen-Tien Chang
 
Overview of Qt/QtQuick Testing, and Study for Qt GUI Automation Testing by Qt...
Overview of Qt/QtQuick Testing, and Study for Qt GUI Automation Testing by Qt...Overview of Qt/QtQuick Testing, and Study for Qt GUI Automation Testing by Qt...
Overview of Qt/QtQuick Testing, and Study for Qt GUI Automation Testing by Qt...Yun-Lin Huang
 
Kamigo reviews 20191127
Kamigo reviews 20191127Kamigo reviews 20191127
Kamigo reviews 20191127Jia Yu Lin
 
Interact 操作消費者教學範例
Interact 操作消費者教學範例Interact 操作消費者教學範例
Interact 操作消費者教學範例turtleknight
 
02.python.开发最佳实践
02.python.开发最佳实践02.python.开发最佳实践
02.python.开发最佳实践Na Lee
 
测试驱动的前端开发初探
测试驱动的前端开发初探测试驱动的前端开发初探
测试驱动的前端开发初探hua qiu
 
Test driven-frontend-develop
Test driven-frontend-developTest driven-frontend-develop
Test driven-frontend-developfangdeng
 
广告技术部自动化测试介绍.pdf
广告技术部自动化测试介绍.pdf广告技术部自动化测试介绍.pdf
广告技术部自动化测试介绍.pdfbj_qa
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Will Huang
 

Similar to Python 类型检查实践 (12)

⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨
 
Python 1-簡介
Python 1-簡介Python 1-簡介
Python 1-簡介
 
Overview of Qt/QtQuick Testing, and Study for Qt GUI Automation Testing by Qt...
Overview of Qt/QtQuick Testing, and Study for Qt GUI Automation Testing by Qt...Overview of Qt/QtQuick Testing, and Study for Qt GUI Automation Testing by Qt...
Overview of Qt/QtQuick Testing, and Study for Qt GUI Automation Testing by Qt...
 
Kamigo reviews 20191127
Kamigo reviews 20191127Kamigo reviews 20191127
Kamigo reviews 20191127
 
Interact 操作消費者教學範例
Interact 操作消費者教學範例Interact 操作消費者教學範例
Interact 操作消費者教學範例
 
02.python.开发最佳实践
02.python.开发最佳实践02.python.开发最佳实践
02.python.开发最佳实践
 
测试驱动的前端开发初探
测试驱动的前端开发初探测试驱动的前端开发初探
测试驱动的前端开发初探
 
Test driven-frontend-develop
Test driven-frontend-developTest driven-frontend-develop
Test driven-frontend-develop
 
广告技术部自动化测试介绍.pdf
广告技术部自动化测试介绍.pdf广告技术部自动化测试介绍.pdf
广告技术部自动化测试介绍.pdf
 
Python lesson 1
Python lesson 1Python lesson 1
Python lesson 1
 
Tip for Editors
Tip for EditorsTip for Editors
Tip for Editors
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)
 

Python 类型检查实践