python里使用callable函数

xiaoxiao2021-02-28  96

可以通过下面的例子来学习一下什么样的对象是可以调用的:

# File: builtin-callable-example-1.py def dump(function): if callable(function): print(function, "is callable") else: print(function, "is *not* callable") class A: def method(self, value): return value class B(A): def __call__(self, value): return value a = A() b = B() dump(0) # simple objects dump("string") dump(callable) dump(dump) # function dump(A) # classes dump(B) dump(B.method) dump(a) # instances dump(b) dump(b.method)

输出结果如下:

== RESTART: D:/work/csdn/python_Game1/example/builtin-callable-example-1.py ==0 is *not* callablestring is *not* callable<built-in function callable> is callable<function dump at 0x0000021FDDD53E18> is callable<class '__main__.A'> is callable<class '__main__.B'> is callable<function A.method at 0x0000021FDE740BF8> is callable<__main__.A object at 0x0000021FDE6F22B0> is *not* callable<__main__.B object at 0x0000021FDE6F24A8> is callable<bound method A.method of <__main__.B object at 0x0000021FDE6F24A8>> is callable>>> 

在这里值得注意是A和B对象都是可以调用的,但是A的实例对象不能调用,因为它没有实现__call__方法。

Python游戏开发入门

http://edu.csdn.net/course/detail/5690

你也能动手修改C编译器

http://edu.csdn.net/course/detail/5582

纸牌游戏开发

http://edu.csdn.net/course/detail/5538 

五子棋游戏开发

http://edu.csdn.net/course/detail/5487RPG游戏从入门到精通http://edu.csdn.net/course/detail/5246WiX安装工具的使用http://edu.csdn.net/course/detail/5207俄罗斯方块游戏开发http://edu.csdn.net/course/detail/5110boost库入门基础http://edu.csdn.net/course/detail/5029Arduino入门基础http://edu.csdn.net/course/detail/4931Unity5.x游戏基础入门http://edu.csdn.net/course/detail/4810TensorFlow API攻略http://edu.csdn.net/course/detail/4495TensorFlow入门基本教程http://edu.csdn.net/course/detail/4369C++标准模板库从入门到精通 http://edu.csdn.net/course/detail/3324跟老菜鸟学C++http://edu.csdn.net/course/detail/2901跟老菜鸟学pythonhttp://edu.csdn.net/course/detail/2592在VC2015里学会使用tinyxml库http://edu.csdn.net/course/detail/2590在Windows下SVN的版本管理与实战 http://edu.csdn.net/course/detail/2579Visual Studio 2015开发C++程序的基本使用 http://edu.csdn.net/course/detail/2570在VC2015里使用protobuf协议http://edu.csdn.net/course/detail/2582在VC2015里学会使用MySQL数据库http://edu.csdn.net/course/detail/2672

转载请注明原文地址: https://www.6miu.com/read-80544.html

最新回复(0)