Python - typing 模块 —— Callable
前言
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
https://www.cnblogs.com/poloyy/p/15153886.html
Callable
是一个可调用对象类型
查看是否可调用
语法
isinstance(对象, Callable) # 返回True或False
栗子
# 最简单的函数
def print_name(name: str):
print(name) # 判断函数是否可调用
print(isinstance(print_name, Callable)) x = 1
print(isinstance(x, Callable)) # 输出结果
True
False
函数是可调用的,所以是 True,而变量不是可调用对象,所以是 False
Callable 作为函数参数
看看 Callable 的源码
Callable type; Callable[[int], str] is a function of (int) -> str.
- 第一个类型(int)代表参数类型
- 第二个类型(str)代表返回值类型
栗子
def print_name(name: str):
print(name) # Callable 作为函数参数使用,其实只是做一个类型检查的作用,检查传入的参数值 get_func 是否为可调用对象
def get_name(get_func: Callable[[str], None]):
return get_func vars = get_name(print_name)
vars("test") # 等价写法,其实就是将函数作为参数传入
def get_name_test(func):
return func vars2 = get_name_test(print_name)
vars2("小菠萝") # 输出结果
test
小菠萝
Callable 作为函数返回值
# Callable 作为函数返回值使用,其实只是做一个类型检查的作用,看看返回值是否为可调用对象
def get_name_return() -> Callable[[str], None]:
return print_name vars = get_name_return()
vars("test") # 等价写法,相当于直接返回一个函数对象
def get_name_test():
return print_name vars2 = get_name_test()
vars2("小菠萝") # 输出结果
test
小菠萝
TypeVar 泛型
https://www.cnblogs.com/poloyy/p/15154196.html
Any Type
https://www.cnblogs.com/poloyy/p/15158613.html
Python - typing 模块 —— Callable的更多相关文章
- Python - typing 模块 —— 常用类型提示
前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...
- Python - typing 模块 —— 类型别名
前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...
- Python - typing 模块 —— NewType
前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...
- Python - typing 模块 —— TypeVar 泛型
前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...
- Python - typing 模块 —— Any Type
前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...
- Python - typing 模块 —— Union
前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...
- Python - typing 模块 —— Optional
前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...
- python类型检测最终指南--Typing模块的使用
正文共:30429 字 预计阅读时间:76分钟 原文链接:https://realpython.com/python-type-checking/ 作者:Geir Arne Hjelle 译者:陈祥安 ...
- python进阶(21)typing模块--类型提示支持
typing介绍 Python是一门弱类型的语言,很多时候我们可能不清楚函数参数的类型或者返回值的类型,这样会导致我们在写完代码一段时间后回过头再看代码,忘记了自己写的函数需要传什么类型的参数,返 ...
随机推荐
- mysql中,一个数字加上null,结果为null
在mysql中,一个数字加上null,结果为null. 这个问题是我用update语句时遇见的,就像下边的例子 update tableName set number = number + x 这里的 ...
- python之学生信息管理系统
1 #!usr/bin/python 2 #encoding=utf-8 3 4 #1. 打印学生管理系统界面 5 def printStd(): 6 print ("*"*50) ...
- Thymeleaf模板引擎语法
th:text 用于显示值 th:object 接收后台传来的对象 th:action 提交表单 th:value 绑定值 th:field 绑定 ...
- 使用crt连接linux慢
1.主要原因是linux中启用了 修改sshd的配置文件把其中dns解析设置为no即可,操作如下: [root@dong ~]# vi /etc/ssh/sshd_config 查找: #UseDNS ...
- 「AGC027D」Modulo Matrix
「AGC027D」Modulo Matrix 传送门 神仙构造题. 首先考虑一个非常自然的思路,我们把棋盘黑白染色后会变成一个二分图,黑色棋子只会与白色棋子相邻. 也就是说,我们可以将二分图的一部随便 ...
- [源码解析] 深度学习分布式训练框架 horovod (16) --- 弹性训练之Worker生命周期
[源码解析] 深度学习分布式训练框架 horovod (16) --- 弹性训练之Worker生命周期 目录 [源码解析] 深度学习分布式训练框架 horovod (16) --- 弹性训练之Work ...
- excel带格式复制python
openpyxl 复制cell单元格包括所有样式 target_cell.data_type = source_cell.data_type target_cell.fill = copy(sourc ...
- POJ3615-Floyd
http://poj.org/problem?id=3615 因为只需要求所在路径的最大高度的最小值,而且n<=300,我们可以用floyd跑. g[i][j]=min(g[i][j],max( ...
- argparse模块基本用法
argparse模块基本用法 在 python 编写的程序中,我们经常会看到的 argparse 相关代码,而它究竟怎么使用呢?接招! argparse 是一个命令行参数解析模块 现在提出需求,我需要 ...
- Jenkins(8080)未授权访问
下载地址http://mirrors.jenkins.io/debian/ 测试 浏览器访问http://localhost:8080/manage可以直接访问 点击脚本命令行 println &qu ...