PhoneGap中文网

 找回密码
 立即注册
查看: 19024|回复: 0
打印 上一主题 下一主题

Python中callable函数如何使用?

[复制链接]

87

主题

87

帖子

327

积分

中级会员

Rank: 3Rank: 3

积分
327
跳转到指定楼层
楼主
发表于 2017-9-4 20:03:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本文和大家分享的主要是python中使用callable函数相关内容,一起来看看吧,希望对大家学习python有所帮助。
  可以通过下面的例子来学习一下什么样的对象是可以调用的:
  # 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* callable
  string is *not* callable
  is callable
  is callable
  is callable
  is callable
  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
  >>>
  在这里值得注意是AB对象都是可以调用的,但是A的实例对象不能调用,因为它没有实现__call__方法。
来源:大坡3D软件开发

it营
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

ionic4视频教程

Archiver|手机版|小黑屋| PhoneGap中文网 ( 京ICP备13027796号-1 )  

GMT+8, 2024-7-27 16:59 , Processed in 0.041566 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表