Python3中的内置函数callable接受一个对象参数,如果此对象参数看起来可调用,则callable函数返回True,否则返回False如果返回True,则调用仍有可能失败;但如果返回False,则调用对象将永远不会成功。

      类是可调用的(调用类会返回一个新实例),如果它们的类具有__call__方法,则实例是可调用的。

      可以使用()运算符调用的任何(或任何对象)都称为可调用对象

      在Python3中,一切都是对象,如函数、方法甚至类。

      所有内置函数都是可调用的。

      所有用户定义的函数也是可调用的。

      lambda函数也是可调用的。

      用户定义的类也是可调用的。

      所有内置方法也是可调用的,如list类包含insert,remove等内置方法,同样,int, float等也都有自己的方法。

      以下为测试代码:

var = 2
if var == 1:# reference: https://www.geeksforgeeks.org/callable-in-python/def Geek():return 5# an object is created of Geek()let = Geek; print(callable(let)) # Truelet2 = Geek(); print(callable(let2)) # False# a test variablenum = 5 * 5; print(callable(num)) # Falseclass Geek2:def __call__(self):print('Hello GeeksforGeeks 2')# Suggests that the Geek2 class is callableprint(callable(Geek2)) # True# This proves that class is callableGeekObject2 = Geek2()# an instance of a class with a __call__ methodGeekObject2() # this is calling the __call__ method, Hello GeeksforGeeks 2class Geek3:def testFunc(self):print('Hello GeeksforGeeks 3')# Suggests that the Geek3 class is callableprint(callable(Geek3)) # True# the Geek3 class is callable, but the instance of Geek3 is not callable() and it returns a runtime errorGeekObject3 = Geek3()# The object will be created but returns an error on callingGeekObject3() # TypeError: 'Geek3' object is not callable
elif var == 2:# reference: https://pythonsimplified.com/what-is-a-callable-in-python/# 内置函数也是函数,因此它们也是可调用的,即callable()返回True. 所有的内置函数都是可调用的print(callable(min)) # Trueprint(callable(max)) # Trueprint(callable(print)) # Trueprint(callable(int)) # True, int classprint(callable(float)) # True, float classprint(callable(str)) # True, str classprint(callable(list)) # True, list class# 所有用户定义的函数也是可调用的def hello(user):print(f"Welcome to Python Simplified, {user}")print(callable(hello)) # True# lambda函数也是可调用的print(callable(lambda x: x**2)) # True# 用户定义的类也是可调用的class Rectangle:def __init__(self, width, height):self.height = heightself.width = widthdef area(self):return self.width * self.areaprint(callable(Rectangle)) # True# 所有内置方法也是可调用的my_list = [1,2,3,4,5]print(callable(my_list.append)) # Trueprint("test finish")

GitHub:http://github.com/fengbingchun/Python_Test

Python3中内置函数callable介绍相关推荐

  1. Python中内置函数的介绍

    内置函数的功能介绍 常用内置函数如下: 1.abs() 绝对值 格式:abs(x) 例如:print(abs(-18))>>> 18 返回值:number #该函数主要用于数值类的操 ...

  2. python中的内置函数getattr()介绍及示例

    python中的内置函数getattr()介绍及示例 其实getattr()这个方法最主要的作用是实现反射机制.也就是说可以通过字符串获取方法实例.这样,你就可以把一个类可能要调用的方法放在配置文件里 ...

  3. python max函数_Python max内置函数详细介绍

    Python max内置函数 max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) Return the largest ite ...

  4. linux awk 内置函数详细介绍(实例)

    原文地址为: linux awk 内置函数详细介绍(实例) 这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中 ...

  5. Python3.9的69个内置函数(内建函数)介绍,并附简单明了的示例代码

    Python 解释器内置(内建)了很多函数,这篇博文对它们进行逐一介绍,博主觉得有必要给出示例代码的会给出示例代码. 声明:博主(昊虹图像算法)写这篇博文时,用的Python的版本号为3.9.10. ...

  6. python3.6中内置函数变化

    最近学习发现,python3.x比之与python2.x,许多内置要么不再是内置函数,要么已经改变调用方式.因此决定把已知的变化写下,以作参考. reduce(function,iterable): ...

  7. PromQL 中内置函数介绍

    Prometheus 提供了其它大量的内置函数,可以对时序数据进行丰富的处理. 某些函数有默认的参数,例如:year(v=vector(time()) instant-vector).其中参数 v 是 ...

  8. python 中内置函数

    python中有很多内置函数下面分类介绍 1.数值函数 abs() 绝对值 max() 获取一组数据的最大值 min() 获取一组数据的最小值 len() 获取一个序列,或字符串的长度 divmod( ...

  9. python中内置函数的用法_python中str内置函数用法总结

    大家在使用python的过程中,应该在敲代码的时候经常遇到str内置函数,为了防止大家搞混,本文整理归纳了str内置函数.1字符串查找类:find.index:2.字符串判断类:islower.isa ...

最新文章

  1. 计算机组成原理-第一章
  2. Android 面试题目之 关于AbstractQueuedSynchronizer的注解翻译
  3. SAP最佳业务实践:重复制造(149)-4发料
  4. 【整理】强化学习与MDP
  5. Win11系统使用Excel表格的时候很卡怎么办
  6. CVPR 2021 接收论文临时列表!27%接受率!
  7. 洛谷P4720 【模板】扩展卢卡斯
  8. 局部变量是线程安全的,原因是什么
  9. 越成功的人越能直面人性的欲望
  10. BZOJ2142: 礼物
  11. LeetCode 105. 从前序与中序遍历序列构造二叉树(递归)
  12. matlab在伪随机码,MATLAB在伪随机码的生成及仿真中的应用
  13. 如何在百度文库复制内容
  14. GUI 图形用户界面编程(一)
  15. docker ps3netsrv_QNAP 威联通 NAS TS-212P应用系列 篇一:实践基于QNAP平台搭建PS3NETSRV服务...
  16. (瑞萨,norti系统,partner-jet2) 实时系统下DMAC使用的注意事项
  17. 万字长文看懂商业智能(BI)|推荐收藏
  18. 2021年12月13日|14日|15日|16日|17日
  19. #Paper Reading# Efficient Heterogeneous Collaborative Filtering without NS for Recommendation
  20. 资产计提折旧日志查询(AFBP)和数据反查(FB03 / AW01N)

热门文章

  1. 使用Python,OpenCV进行Tesseract-OCR绑定及识别
  2. OpenCV-Python绑定如何工作及如何扩展新模块到Python
  3. OpenCV最经典的3种颜色空间(cv2.cvtColor)及互相转换
  4. 洛谷P4568 飞行路线 最短路k条免费
  5. PCL滤波介绍(3)
  6. keras中conv2d,conv2dTranspose的Padding详细介绍
  7. Error: “incorrect inclusion of a cudart header file”
  8. 线性连续时间状态空间模型的离散化及实例
  9. 【力扣网练习题】实现 strStr()
  10. 使用希捷DiscWizard格式化3TB硬盘并分区