- Python 基础
- Python - 首页
- Python - 概述
- Python - 历史
- Python - 特性
- Python vs C++
- Python - Hello World 程序
- Python - 应用领域
- Python - 解释器
- Python - 环境搭建
- Python - 虚拟环境
- Python - 基本语法
- Python - 变量
- Python - 数据类型
- Python - 类型转换
- Python - Unicode 系统
- Python - 字面量
- Python - 运算符
- Python - 算术运算符
- Python - 比较运算符
- Python - 赋值运算符
- Python - 逻辑运算符
- Python - 位运算符
- Python - 成员运算符
- Python - 身份运算符
- Python - 运算符优先级
- Python - 注释
- Python - 用户输入
- Python - 数字
- Python - 布尔值
- Python 控制语句
- Python - 控制流
- Python - 决策
- Python - if 语句
- Python - if else
- Python - 嵌套 if
- Python - match-case 语句
- Python - 循环
- Python - for 循环
- Python - for-else 循环
- Python - while 循环
- Python - break 语句
- Python - continue 语句
- Python - pass 语句
- Python - 嵌套循环
- Python 函数 & 模块
- Python - 函数
- Python - 默认参数
- Python - 关键字参数
- Python - 仅限关键字参数
- Python - 位置参数
- Python - 仅限位置参数
- Python - 可变参数
- Python - 变量作用域
- Python - 函数注解
- Python - 模块
- Python - 内置函数
- Python 字符串
- Python - 字符串
- Python - 字符串切片
- Python - 修改字符串
- Python - 字符串连接
- Python - 字符串格式化
- Python - 转义字符
- Python - 字符串方法
- Python - 字符串练习
- Python 列表
- Python - 列表
- Python - 访问列表元素
- Python - 修改列表元素
- Python - 添加列表元素
- Python - 删除列表元素
- Python - 遍历列表
- Python - 列表推导式
- Python - 排序列表
- Python - 复制列表
- Python - 合并列表
- Python - 列表方法
- Python - 列表练习
- Python 元组
- Python - 元组
- Python - 访问元组元素
- Python - 更新元组
- Python - 解包元组
- Python - 遍历元组
- Python - 合并元组
- Python - 元组方法
- Python - 元组练习
- Python 集合
- Python - 集合
- Python - 访问集合元素
- Python - 添加集合元素
- Python - 删除集合元素
- Python - 遍历集合
- Python - 合并集合
- Python - 复制集合
- Python - 集合运算符
- Python - 集合方法
- Python - 集合练习
- Python 字典
- Python - 字典
- Python - 访问字典元素
- Python - 修改字典元素
- Python - 添加字典元素
- Python - 删除字典元素
- Python - 字典视图对象
- Python - 遍历字典
- Python - 复制字典
- Python - 嵌套字典
- Python - 字典方法
- Python - 字典练习
- Python 数组
- Python - 数组
- Python - 访问数组元素
- Python - 添加数组元素
- Python - 删除数组元素
- Python - 遍历数组
- Python - 复制数组
- Python - 反转数组
- Python - 排序数组
- Python - 合并数组
- Python - 数组方法
- Python - 数组练习
- Python 文件处理
- Python - 文件处理
- Python - 写入文件
- Python - 读取文件
- Python - 重命名和删除文件
- Python - 目录
- Python - 文件方法
- Python - OS 文件/目录方法
- Python - OS 路径方法
- 面向对象编程
- Python - OOPs 概念
- Python - 类 & 对象
- Python - 类属性
- Python - 类方法
- Python - 静态方法
- Python - 构造函数
- Python - 访问修饰符
- Python - 继承
- Python - 多态
- Python - 方法重写
- Python - 方法重载
- Python - 动态绑定
- Python - 动态类型
- Python - 抽象
- Python - 封装
- Python - 接口
- Python - 包
- Python - 内部类
- Python - 匿名类和对象
- Python - 单例类
- Python - 包装类
- Python - 枚举
- Python - 反射
- Python 错误 & 异常
- Python - 语法错误
- Python - 异常
- Python - try-except 块
- Python - try-finally 块
- Python - 抛出异常
- Python - 异常链
- Python - 嵌套 try 块
- Python - 用户自定义异常
- Python - 日志记录
- Python - 断言
- Python - 内置异常
- Python 多线程
- Python - 多线程
- Python - 线程生命周期
- Python - 创建线程
- Python - 启动线程
- Python - 加入线程
- Python - 线程命名
- Python - 线程调度
- Python - 线程池
- Python - 主线程
- Python - 线程优先级
- Python - 守护线程
- Python - 线程同步
- Python 同步
- Python - 线程间通信
- Python - 线程死锁
- Python - 中断线程
- Python 网络编程
- Python - 网络编程
- Python - 套接字编程
- Python - URL 处理
- Python - 泛型
- Python 库
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 其他
- Python - 日期 & 时间
- Python - 数学
- Python - 迭代器
- Python - 生成器
- Python - 闭包
- Python - 装饰器
- Python - 递归
- Python - 正则表达式
- Python - PIP
- Python - 数据库访问
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 输出格式化
- Python - 性能测量
- Python - 数据压缩
- Python - CGI 编程
- Python - XML 处理
- Python - GUI 编程
- Python - 命令行参数
- Python - 文档字符串
- Python - JSON
- Python - 发送邮件
- Python - 扩展
- Python - 工具/实用程序
- Python - GUIs
- Python 高级概念
- Python - 抽象基类
- Python - 自定义异常
- Python - 高阶函数
- Python - 对象内部
- Python - 内存管理
- Python - 元类
- Python - 使用元类进行元编程
- Python - 模拟和存根
- Python - Monkey Patching
- Python - 信号处理
- Python - 类型提示
- Python - 自动化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 协程
- Python - 描述符
- Python - 诊断和修复内存泄漏
- Python - 不可变数据结构
- Python 有用资源
- Python - 问答
- Python - 在线测验
- Python - 快速指南
- Python - 参考
- Python - 速查表
- Python - 项目
- Python - 有用资源
- Python - 讨论
- Python 编译器
- NumPy 编译器
- Matplotlib 编译器
- SciPy 编译器
Python - 类型提示
Python 的类型提示在 PEP 484 中引入,旨在将静态类型的优势带入动态类型语言。虽然类型提示不会在运行时强制执行类型检查,但它们提供了一种指定变量、函数参数和返回值的预期类型的方法,这些类型可以通过静态分析工具(例如mypy)进行检查。这增强了代码的可读性,方便了调试,并提高了代码的整体可维护性。
Python 中的类型提示使用注解来指定函数参数、返回值和变量赋值。
Python 的类型提示可用于指定各种类型,例如基本数据类型、集合、复杂类型和自定义用户定义类型。typing 模块提供了许多内置类型来表示这些各种类型:
让我们逐一详细了解每一个。
基本数据类型
在 Python 中,当使用类型提示指定基本类型时,我们可以简单地使用类型的名称作为注解。
示例
以下是使用基本数据类型(例如整数、浮点数、字符串等)的示例:
from typing import Optional
# Integer type
def calculate_square_area(side_length: int) -> int:
return side_length ** 2
# Float type
def calculate_circle_area(radius: float) -> float:
return 3.14 * radius * radius
# String type
def greet(name: str) -> str:
return f"Hello, {name}"
# Boolean type
def is_adult(age: int) -> bool:
return age >= 18
# None type
def no_return_example() -> None:
print("This function does not return anything")
# Optional type (Union of int or None)
def safe_divide(x: int, y: Optional[int]) -> Optional[float]:
if y is None or y == 0:
return None
else:
return x / y
# Example usage
print(calculate_square_area(5))
print(calculate_circle_area(3.0))
print(greet("Alice"))
print(is_adult(22))
no_return_example()
print(safe_divide(10, 2))
print(safe_divide(10, 0))
print(safe_divide(10, None))
执行上述代码后,我们将得到以下输出:
25 28.259999999999998 Hello, Alice True This function does not return anything 5.0 None None
集合类型
在 Python 中,处理列表、元组、字典等集合时,在类型提示中,我们通常使用typing 模块来指定集合类型。
示例
以下是类型提示中使用集合的示例:
from typing import List, Tuple, Dict, Set, Iterable, Generator
# List of integers
def process_numbers(numbers: List[int]) -> List[int]:
return [num * 2 for num in numbers]
# Tuple of floats
def coordinates() -> Tuple[float, float]:
return (3.0, 4.0)
# Dictionary with string keys and integer values
def frequency_count(items: List[str]) -> Dict[str, int]:
freq = {}
for item in items:
freq[item] = freq.get(item, 0) + 1
return freq
# Set of unique characters in a string
def unique_characters(word: str) -> Set[str]:
return set(word)
# Iterable of integers
def print_items(items: Iterable[int]) -> None:
for item in items:
print(item)
# Generator yielding squares of integers up to n
def squares(n: int) -> Generator[int, None, None]:
for i in range(n):
yield i * i
# Example usage
numbers = [1, 2, 3, 4, 5]
print(process_numbers(numbers))
print(coordinates())
items = ["apple", "banana", "apple", "orange"]
print(frequency_count(items))
word = "hello"
print(unique_characters(word))
print_items(range(5))
gen = squares(5)
print(list(gen))
执行上述代码后,我们将得到以下输出:
[2, 4, 6, 8, 10]
(3.0, 4.0)
{'apple': 2, 'banana': 1, 'orange': 1}
{'l', 'e', 'h', 'o'}
0
1
2
3
4
[0, 1, 4, 9, 16]
可选类型
在 Python 中,可选类型用于指示变量可以是指定类型或 None。当函数可能并非总是返回值,或者参数可以接受值或留空时,这尤其有用。
示例
以下是类型提示中使用可选类型的示例:
from typing import Optional
def divide(a: float, b: float) -> Optional[float]:
if b == 0:
return None
else:
return a / b
result1: Optional[float] = divide(10.0, 2.0) # result1 will be 5.0
result2: Optional[float] = divide(10.0, 0.0) # result2 will be None
print(result1)
print(result2)
执行上述代码后,我们将得到以下输出:
5.0 None
联合类型
Python 使用联合类型允许变量接受不同类型的值。当函数或数据结构可以使用各种类型的输入或产生不同类型的输出时,这很有用。
示例
以下是这个示例:
from typing import Union
def square_root_or_none(number: Union[int, float]) -> Union[float, None]:
if number >= 0:
return number ** 0.5
else:
return None
result1: Union[float, None] = square_root_or_none(50)
result2: Union[float, None] = square_root_or_none(-50)
print(result1)
print(result2)
执行上述代码后,我们将得到以下输出:
7.0710678118654755 None
Any 类型
在 Python 中,Any 类型是一种特殊的类型提示,它表示变量可以是任何类型。它实际上禁用了该特定变量或表达式的类型检查。当事先不知道值的类型或处理动态数据时,这可能很有用。
示例
以下是类型提示中使用 Any 类型的示例:
from typing import Any
def print_value(value: Any) -> None:
print(value)
print_value(10)
print_value("hello")
print_value(True)
print_value([1, 2, 3])
print_value({'key': 'value'})
执行上述代码后,我们将得到以下输出:
10
hello
True
[1, 2, 3]
{'key': 'value'}
类型别名
Python 中的类型别名用于为现有类型提供替代名称。它们可以通过为复杂的类型注解或类型的组合提供清晰的名称来使代码更易于阅读。这在处理嵌套结构或长类型提示时尤其有用。
示例
以下是类型提示中使用类型别名的示例:
from typing import List, Tuple
# Define a type alias for a list of integers
Vector = List[int]
# Define a type alias for a tuple of coordinates
Coordinates = Tuple[float, float]
# Function using the type aliases
def scale_vector(vector: Vector, factor: float) -> Vector:
return [int(num * factor) for num in vector]
def calculate_distance(coord1: Coordinates, coord2: Coordinates) -> float:
x1, y1 = coord1
x2, y2 = coord2
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
# Using the type aliases
v: Vector = [1, 2, 3, 4]
scaled_v: Vector = scale_vector(v, 2.5)
print(scaled_v)
c1: Coordinates = (3.0, 4.0)
c2: Coordinates = (6.0, 8.0)
distance: float = calculate_distance(c1, c2)
print(distance)
执行上述代码后,我们将得到以下输出:
[2, 5, 7, 10] 5.0
泛型类型
泛型 创建可以处理任何类型同时保持类型安全性的函数、类或数据结构。typing 模块的 TypeVar 和 Generic 构造使得这成为可能。它们有助于创建可重用的组件,这些组件可以与各种类型一起工作,而不会影响类型检查。
示例
这是一个示例:
from typing import TypeVar, List
# Define a type variable T
T = TypeVar('T')
# Generic function that returns the first element of a list
def first_element(items: List[T]) -> T:
return items[0]
# Example usage
int_list = [1, 2, 3, 4, 5]
str_list = ["apple", "banana", "cherry"]
first_int = first_element(int_list) # first_int will be of type int
first_str = first_element(str_list) # first_str will be of type str
print(first_int)
print(first_str)
执行上述代码后,我们将得到以下输出:
1 apple
可调用类型
Python 的Callable 类型用于表明某种类型是函数或可调用对象。它位于 typing 模块中,允许你定义函数的参数类型和返回类型。这对于高阶函数非常有用。
示例
以下是使用 Callable 类型进行类型提示的示例:
from typing import Callable # Define a function that takes another function as an argument def apply_operation(x: int, y: int, operation: Callable[[int, int], int]) -> int: return operation(x, y) # Example functions to be passed as arguments def add(a: int, b: int) -> int: return a + b def multiply(a: int, b: int) -> int: return a * b # Using the apply_operation function with different operations result1 = apply_operation(5, 3, add) # result1 will be 8 result2 = apply_operation(5, 3, multiply) # result2 will be 15 print(result1) print(result2)
执行上述代码后,我们将得到以下输出:
8 15
字面量类型
Literal 类型用于指定值必须是预定义值集合中的一个。
示例
以下是一个示例:
from typing import Literal
def move(direction: Literal["left", "right", "up", "down"]) -> None:
print(f"Moving {direction}")
move("left") # Valid
move("up") # Valid
执行上述代码后,我们将得到以下输出:
Moving left Moving up
NewType
NewType 是 typing 模块中的一个函数,它允许我们创建从现有类型派生的 distinct 类型。这对于通过区分相同底层类型的不同用途来增强代码类型安全性非常有用。例如,我们可能希望区分用户 ID 和产品 ID,即使两者都表示为整数。
示例
以下是一个示例:
from typing import NewType
# Create new types
UserId = NewType('UserId', int)
ProductId = NewType('ProductId', int)
# Define functions that use the new types
def get_user_name(user_id: UserId) -> str:
return f"User with ID {user_id}"
def get_product_name(product_id: ProductId) -> str:
return f"Product with ID {product_id}"
# Example usage
user_id = UserId(42)
product_id = ProductId(101)
print(get_user_name(user_id)) # Output: User with ID 42
print(get_product_name(product_id)) # Output: Product with ID 101
执行上述代码后,我们将得到以下输出:
User with ID 42 Product with ID 101