零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门
Python 内置函数 callable 用于检查一个对象是否是可调用的,如果函数返回True,object 仍然可能调用失败;但如果返回 False,调用对象 object 绝对不会成功。
一.Python callable 函数简介
语法如下:
'''参数介绍:object : 调用的对象;返回值:返回bool值,如果object对象可以被调用返回true,不能被调用返回false;'''callable(object)''' 参数介绍: object : 调用的对象; 返回值:返回bool值,如果object对象可以被调用返回true,不能被调用返回false; ''' callable(object)''' 参数介绍: object : 调用的对象; 返回值:返回bool值,如果object对象可以被调用返回true,不能被调用返回false; ''' callable(object)
值得注意的是:即便函数返回 true,object 也有可能调用失败,返回 false 意味着觉得不会成功!
对于函数, 方法, lambda 函式, 类, 以及实现了 __call__ 方法的类实例, 它都返回 True。
二.Python callable 函数使用
# !usr/bin/env python# -*- coding:utf-8 _*-"""@Author:猿说编程@Blog(个人博客地址): www.codersrc.com@File:Python issubclass 函数.py@Time:2021/04/30 07:37@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!"""def test(func):# 判断func如果是函数,就执行他,如果不是函数,直接返回# 判断func是否可调用,如果可以调用,就是trueif callable(func):print("函数可以被调用")return funcelse:print("函数不可以被调用")def test2():return 'shuopython.com'if __name__ == "__main__":print(test(test2())) #等价 test(str) 字符串不是对象print("***"*20)print(test(test2)) #等价 test(func) 函数是对象print("***"*20)print(test(123)) #等价 test(int) 整形不是对象'''输出结果:函数不可以被调用None************************************************************函数可以被调用<function test2 at 0x00000252F39D57B8>************************************************************函数不可以被调用None'''# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python issubclass 函数.py @Time:2021/04/30 07:37 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ def test(func): # 判断func如果是函数,就执行他,如果不是函数,直接返回 # 判断func是否可调用,如果可以调用,就是true if callable(func): print("函数可以被调用") return func else: print("函数不可以被调用") def test2(): return 'shuopython.com' if __name__ == "__main__": print(test(test2())) #等价 test(str) 字符串不是对象 print("***"*20) print(test(test2)) #等价 test(func) 函数是对象 print("***"*20) print(test(123)) #等价 test(int) 整形不是对象 ''' 输出结果: 函数不可以被调用 None ************************************************************ 函数可以被调用 <function test2 at 0x00000252F39D57B8> ************************************************************ 函数不可以被调用 None '''# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python issubclass 函数.py @Time:2021/04/30 07:37 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ def test(func): # 判断func如果是函数,就执行他,如果不是函数,直接返回 # 判断func是否可调用,如果可以调用,就是true if callable(func): print("函数可以被调用") return func else: print("函数不可以被调用") def test2(): return 'shuopython.com' if __name__ == "__main__": print(test(test2())) #等价 test(str) 字符串不是对象 print("***"*20) print(test(test2)) #等价 test(func) 函数是对象 print("***"*20) print(test(123)) #等价 test(int) 整形不是对象 ''' 输出结果: 函数不可以被调用 None ************************************************************ 函数可以被调用 <function test2 at 0x00000252F39D57B8> ************************************************************ 函数不可以被调用 None '''
三.猜你喜欢
- Python for循环
- Python 字符串
- Python 列表list
- Python 元组tuple
- Python 字典 dict
- Python 条件推导式
- Python 列表推导式
- Python 字典推导式
- Python 函数声明和调用
- Python 不定长参数 *argc/**kargcs
- Python 匿名函数 lambda
- Python return 逻辑判断表达式
- Python 字符串/列表/元组/字典之间的相互转换
- Python 局部变量和全局变量
- Python type 函数和 isinstance 函数区别
- Python is 和 == 区别
- Python 可变数据类型和不可变数据类型
- Python 浅拷贝和深拷贝
ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容