Commit 407e07ec authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

tweaks

parent fd859e1e
Pipeline #1209 failed with stage
in 0 seconds
...@@ -21,7 +21,7 @@ Files/directories with asterisks (``*``) marks are optional. ...@@ -21,7 +21,7 @@ Files/directories with asterisks (``*``) marks are optional.
│ ├── demo.py # Python modules │ ├── demo.py # Python modules
│ ├── flip_image.py │ ├── flip_image.py
│ ├── scratch.py │ ├── scratch.py
│ └── top_level_interface.py # the top level interface module │ └── api.py # the top level interface module
├── doc # *sphinx-based documentation directory ├── doc # *sphinx-based documentation directory
│ ├── build │ ├── build
│ ├── source │ ├── source
......
%% Cell type:markdown id:697384beaffb53a3 tags: %% Cell type:markdown id:697384beaffb53a3 tags:
# How to do Python type annotation # How to do Python type annotation
%% Cell type:markdown id:4313baabbbf2ecb tags: %% Cell type:markdown id:4313baabbbf2ecb tags:
## built-in types ## built-in types
%% Cell type:code id:5fbe06f15bbcd630 tags: %% Cell type:code id:5fbe06f15bbcd630 tags:
``` python ``` python
a: int = 8 # 整数型标注 a: int = 8 # 整数型标注
b: float = 1.23 # 浮点型标注 b: float = 1.23 # 浮点型标注
c: bool = True # 布尔型标注 c: bool = True # 布尔型标注
d: tuple = (1, 2) # 元组型标注 d: tuple = (1, 2) # 元组型标注
e: tuple[int, float] = (1, 2.34) # 限定类型和元素个数的元组标注 e: tuple[int, float] = (1, 2.34) # 限定类型和元素个数的元组标注
f: tuple[int, ...] = (1, 2, 3, 4, 5., "abc") # 限定类型但不定元素个数的元组型标注 f: tuple[int, ...] = (1, 2, 3, 4, 5) # 限定类型但不定元素个数的元组型标注
g: list = [1, 2, 3] # 一般的列表型标注 g: list = [1, 2, 3] # 一般的列表型标注
h: list[int] = [1, 2, 3] # 对列表型来说不需要标元素个数,但可以明确要求元素类型 h: list[int] = [1, 2, 3] # 对列表型来说不需要标元素个数,但可以明确要求元素类型
``` ```
%% Cell type:markdown id:a06987fbe46de9e4 tags: %% Cell type:markdown id:a06987fbe46de9e4 tags:
## `typing` 模块 ## `typing` 模块
对于一些内建类型来说是可以直接用内建类型来标注的,例如以下两句是相等的 对于一些内建类型来说是可以直接用内建类型来标注的,例如以下两句是相等的
%% Cell type:code id:c9d78355e0521512 tags: %% Cell type:code id:c9d78355e0521512 tags:
``` python ``` python
l1: list[int] = [1, 2, 3] l1: list[int] = [1, 2, 3]
from typing import List from typing import List
l2: List[int] = [1, 2, 3] l2: List[int] = [1, 2, 3]
``` ```
%% Cell type:markdown id:8cab70562a62c99 tags: %% Cell type:markdown id:8cab70562a62c99 tags:
## Union, Optional和Any ## Union, Optional和Any
- Union表示并集,Union[float, int]表示可以是float也可以是int - Union表示并集,Union[float, int]表示可以是float也可以是int
- Optional表示可选,Optional[int]表示可以是None也可以是int - Optional表示可选,Optional[int]表示可以是None也可以是int
- Any表示任意类型 - Any表示任意类型
%% Cell type:markdown id:ca8b1d813d84ad8c tags: %% Cell type:markdown id:ca8b1d813d84ad8c tags:
## numpy.typing ## numpy.typing
我们经常会用到numpy.ndarray进行科学计算,可以有以下几种标注方式: 我们经常会用到numpy.ndarray进行科学计算,可以有以下几种标注方式:
1. 直接用np.ndarray进行标注, 此时不限定类型(dtype) 1. 直接用np.ndarray进行标注, 此时不限定类型(dtype)
%% Cell type:code id:6ebd28b4f4c84ab4 tags: %% Cell type:code id:6ebd28b4f4c84ab4 tags:
``` python ``` python
import numpy as np import numpy as np
x1: np.ndarray = np.arange(10) x1: np.ndarray = np.arange(10)
``` ```
%% Cell type:markdown id:622cde4d8d3fb34a tags: %% Cell type:markdown id:622cde4d8d3fb34a tags:
2. 限定类型时不能使用np.ndarray而必须用numpy.typing模块 2. 限定类型时不能使用np.ndarray而必须用numpy.typing模块
%% Cell type:code id:36d4e0be053bbd1a tags: %% Cell type:code id:36d4e0be053bbd1a tags:
``` python ``` python
import numpy.typing as npt import numpy.typing as npt
x2: npt.NDArray = np.arange(10, dtype=float) x2: npt.NDArray = np.arange(10, dtype=float)
x3: npt.NDArray[np.int_] = np.arange(10, dtype=int) x3: npt.NDArray[np.int_] = np.arange(10, dtype=int)
x4: npt.NDArray[np.float_] = np.arange(10, dtype=float) x4: npt.NDArray[np.float_] = np.arange(10, dtype=float)
x5: npt.NDArray[np.float32] = np.arange(10, dtype=np.float32) x5: npt.NDArray[np.float32] = np.arange(10, dtype=np.float32)
x6: npt.NDArray[np.bool_] = np.ones(10, dtype=bool) x6: npt.NDArray[np.bool_] = np.ones(10, dtype=bool)
x7: npt.NDArray[np.complex_] = np.ones(10, dtype=np.complex_) x7: npt.NDArray[np.complex_] = np.ones(10, dtype=np.complex_)
``` ```
%% Cell type:code id:47b6d72d76ee84a1 tags: %% Cell type:code id:47b6d72d76ee84a1 tags:
``` python ``` python
``` ```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment