前言

typing 是在 python 3.5 才有的模块

前置学习

Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html

常用类型提示

https://www.cnblogs.com/poloyy/p/15150315.html

类型别名

https://www.cnblogs.com/poloyy/p/15153883.html

NewType

可以自定义创一个新类型

  • 主要用于类型检查
  • NewType(name, tp) 返回一个函数,这个函数返回其原本的值
  • 静态类型检查器会将新类型看作是原始类型的一个子类
  • tp 就是原始类型

实际栗子

# NewType
from typing import NewType UserId = NewType('UserId', int) def name_by_id(user_id: UserId) -> str:
print(user_id) UserId('user') # Fails type check
num = UserId(5) # type: int name_by_id(42) # Fails type check
name_by_id(UserId(42)) # OK print(type(UserId(5))) # 输出结果
42
42
<class 'int'>

可以看到 UserId 其实也是 int 类型

类型检查

使用 UserId 类型做算术运算,得到的是 int 类型数据

# 'output' is of type 'int', not 'UserId'
output = UserId(23413) + UserId(54341)
print(output)
print(type(output)) # 输出结果
77754
<class 'int'>

Callable

https://www.cnblogs.com/poloyy/p/15154008.html

TypeVar 泛型

https://www.cnblogs.com/poloyy/p/15154196.html

Any Type

https://www.cnblogs.com/poloyy/p/15158613.html

Python - typing 模块 —— NewType的更多相关文章

  1. Python - typing 模块 —— 常用类型提示

    前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...

  2. Python - typing 模块 —— 类型别名

    前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...

  3. Python - typing 模块 —— Callable

    前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...

  4. Python - typing 模块 —— TypeVar 泛型

    前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...

  5. Python - typing 模块 —— Any Type

    前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...

  6. Python - typing 模块 —— Union

    前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...

  7. Python - typing 模块 —— Optional

    前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...

  8. python进阶(21)typing模块--类型提示支持

    typing介绍   Python是一门弱类型的语言,很多时候我们可能不清楚函数参数的类型或者返回值的类型,这样会导致我们在写完代码一段时间后回过头再看代码,忘记了自己写的函数需要传什么类型的参数,返 ...

  9. python类型检测最终指南--Typing模块的使用

    正文共:30429 字 预计阅读时间:76分钟 原文链接:https://realpython.com/python-type-checking/ 作者:Geir Arne Hjelle 译者:陈祥安 ...

随机推荐

  1. 1.3.9、通过权重 Weight匹配

    server: port: 8080 spring: application: name: gateway cloud: gateway: routes: - id: guo-system1 uri: ...

  2. 学习/etc/group /etc/passwd 和 /etc/shadow

    /etc/passwd 管理用户信息的系统文件 /etc/shadow 管理用户密码信息的系统文件 /etc/group 管理用户组信息的系统文件 1./etc/group 将用户分组是Linux系统 ...

  3. 42. Trapping Rain Water [dp][stack]

    description: Given n non-negative integers representing an elevation map where the width of each bar ...

  4. 『心善渊』Selenium3.0基础 — 27、unittest跳过测试的使用

    目录 1.什么是跳过测试 2.常用的跳过测试方法和装饰器 3.跳过测试示例 4.TestCase.skipTest()方法 1.什么是跳过测试 当测试用例写完后,有些模块有改动时候,会影响到部分用例的 ...

  5. TCP/IP 5层协议簇/协议栈

    TCP/IP 5层协议簇/协议栈 数据/PDU 应用层 PC.防火墙 数据段/段Fragment 传输层 防火墙 报文/包/IP包packet 网络层 路由器 帧Frame 数据链路层 交换机.网卡 ...

  6. scrapy设置自己的headers referer字段

    1.在middlewares中添加自己的新类: class Mylei(object): def process_request(self,request,spider): referer=reque ...

  7. Rust安装-运行第一个程序-hello_world

    Rust官网:https://rust-lang.org/ 安装 点击install,选择版本 选择相对应的版本进行下载 我这里下载的是windows系统,运行下载好的exe文件,根据需要选择选对应的 ...

  8. Tutorial_6 运行结果

    1.buyer_favorites.txt 2.代码 package mapreduce; import java.io.IOException; import java.util.Iterator; ...

  9. 如何监控 Log4j2 异步日志遇到写入瓶颈

    如何监控 Log4j2 异步日志遇到写入瓶颈 在之前的一篇文章中(一次鞭辟入里的 Log4j2 异步日志输出阻塞问题的定位),我们详细分析了一个经典的 Log4j2 异步日志阻塞问题的定位,主要原因还 ...

  10. Python_结合Re正则模块爬虫

    ##### 爬取古诗文import reimport requestsdef parse_page(url): headers = { 'User-Agent':'Mozilla/5.0 (Windo ...