Python中有趣在__call__函数


Posted in Python onJune 21, 2015

Python中有一个有趣的语法,只要定义类型的时候,实现__call__函数,这个类型就成为可调用的。
换句话说,我们可以把这个类型的对象当作函数来使用,相当于 重载了括号运算符。

class g_dpm(object):
def __init__(self, g):
self.g = g
def __call__(self, t):
return (self.g*t**2)/2

计算地球场景的时候,我们就可以令e_dpm = g_dpm(9.8),s = e_dpm(t)。

class Animal(object):
  def __init__(self, name, legs):
    self.name = name
    self.legs = legs
    self.stomach = []    
 
  def __call__(self,food):
    self.stomach.append(food)
 
  def poop(self):
    if len(self.stomach) > 0:
      return self.stomach.pop(0)
 
  def __str__(self):    
    return 'A animal named %s' % (self.name)    
 
cow = Animal('king', 4) #We make a cow
dog = Animal('flopp', 4) #We can make many animals
print 'We have 2 animales a cow name %s and dog named %s,both have %s legs' % (cow.name, dog.name, cow.legs)
print cow #here __str__ metod work
 
#We give food to cow
cow('gras')
print cow.stomach
 
#We give food to dog
dog('bone')
dog('beef')
print dog.stomach
 
#What comes inn most come out
print cow.poop()
print cow.stomach #Empty stomach
 
'''-->output
We have 2 animales a cow name king and dog named flopp,both have 4 legs
A animal named king
['gras']
['bone', 'beef']
gras
[]
'''
Python 相关文章推荐
python使用ctypes模块调用windowsapi获取系统版本示例
Apr 17 Python
Python实现Sqlite将字段当做索引进行查询的方法
Jul 21 Python
遍历python字典几种方法总结(推荐)
Sep 11 Python
详解Python中的相对导入和绝对导入
Jan 06 Python
Python把csv数据写入list和字典类型的变量脚本方法
Jun 15 Python
详解Django中类视图使用装饰器的方式
Aug 12 Python
python3通过selenium爬虫获取到dj商品的实例代码
Apr 25 Python
基于python实现文件加密功能
Jan 06 Python
python清空命令行方式
Jan 13 Python
python烟花效果的代码实例
Feb 25 Python
django实现后台显示媒体文件
Apr 07 Python
filter使用python3代码进行迭代元素的实例详解
Dec 03 Python
Python的装饰器模式与面向切面编程详解
Jun 21 #Python
Python安装第三方库的3种方法
Jun 21 #Python
Python实现线程池代码分享
Jun 21 #Python
Python os模块学习笔记
Jun 21 #Python
Pthon批量处理将pdb文件生成dssp文件
Jun 21 #Python
Python实现删除文件但保留指定文件
Jun 21 #Python
Python ValueError: invalid literal for int() with base 10 实用解决方法
Jun 21 #Python
You might like
PHP下通过exec获得计算机的唯一标识[CPU,网卡 MAC地址]
2011/06/09 PHP
php学习笔记之面向对象编程
2012/12/29 PHP
PHP实现一维数组转二维数组的方法
2015/02/25 PHP
CodeIgniter记录错误日志的方法全面总结
2016/05/17 PHP
PHP下载大文件失败并限制下载速度的实例代码
2019/05/10 PHP
laravel框架如何设置公共头和公共尾
2019/10/22 PHP
javascript中删除指定数组中指定的元素的代码
2011/02/12 Javascript
Javascript中定义方法的另类写法(批量定义js对象的方法)
2011/02/25 Javascript
使用jQuery实现的掷色子游戏动画效果
2014/03/14 Javascript
轻松创建nodejs服务器(9):实现非阻塞操作
2014/12/18 NodeJs
JavaScript实现DIV层拖动及动态增加新层的方法
2015/05/12 Javascript
javascript+HTML5自定义元素播放焦点图动画
2016/02/21 Javascript
利用Js+Css实现折纸动态导航效果实例源码
2017/01/25 Javascript
详解Javascript百度地图接口开发文档中的类和方法
2017/02/07 Javascript
Angularjs的启动过程分析
2017/07/18 Javascript
JS写XSS cookie stealer来窃取密码的步骤详解
2017/11/20 Javascript
Vue使用axios出现options请求方法
2019/05/30 Javascript
实用Javascript调试技巧分享(小结)
2019/06/18 Javascript
layui实现根据table数据判断按钮显示情况的方法
2019/09/26 Javascript
jQuery鼠标滑过横向时间轴样式(代码详解)
2019/11/01 jQuery
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
2020/12/25 Vue.js
python实现根据图标提取分类应用程序实例
2014/09/28 Python
Python装饰器(decorator)定义与用法详解
2018/02/09 Python
使用requests库制作Python爬虫
2018/03/25 Python
使用Django和Python创建Json response的方法
2018/03/26 Python
解决python xx.py文件点击完之后一闪而过的问题
2019/06/24 Python
Pytorch 计算误判率,计算准确率,计算召回率的例子
2020/01/18 Python
CSS3实现圆角、阴影、透明效果并兼容各大浏览器
2014/08/08 HTML / CSS
仿CSDN Blog返回页面顶部功能实现原理及代码
2013/06/30 HTML / CSS
意大利团购网站:Groupon意大利
2016/10/11 全球购物
优秀幼教自荐信
2014/02/03 职场文书
党的群众路线教育实践活动批评与自我批评
2014/02/16 职场文书
2014年体育工作总结
2014/11/24 职场文书
2014年学校财务工作总结
2014/12/06 职场文书
诚信教育主题班会
2015/08/13 职场文书
使用nginx配置访问wgcloud的方法
2021/06/26 Servers