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生成器的使用方法
Nov 21 Python
在Windows服务器下用Apache和mod_wsgi配置Python应用的教程
May 06 Python
Python多进程分块读取超大文件的方法
Apr 13 Python
TensorFlow入门使用 tf.train.Saver()保存模型
Apr 24 Python
Python实现确认字符串是否包含指定字符串的实例
May 02 Python
python爬虫之urllib,伪装,超时设置,异常处理的方法
Dec 19 Python
python matplotlib折线图样式实现过程
Nov 04 Python
python 装饰器功能与用法案例详解
Mar 06 Python
PyTorch预训练Bert模型的示例
Nov 17 Python
Python3利用scapy局域网实现自动多线程arp扫描功能
Jan 21 Python
python随机打印成绩排名表
Jun 23 Python
Python装饰器的练习题
Nov 23 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 动态执行带有参数的类方法
2009/04/10 PHP
PHP SplObjectStorage使用实例
2015/05/12 PHP
PHP中递归的实现实例详解
2017/11/14 PHP
php和vue配合使用技巧和方法
2019/05/09 PHP
做网页的一些技巧(续)
2007/02/01 Javascript
优秀js开源框架-jQuery使用手册(1)
2007/03/10 Javascript
基于JQuery框架的AJAX实例代码
2009/11/03 Javascript
jQuery源码中的chunker 正则过滤符分析
2012/07/31 Javascript
两个多选select(multiple左右)添加、删除选项和取值实例
2014/05/12 Javascript
基于AngularJS实现页面滚动到底自动加载数据的功能
2015/10/16 Javascript
jQuery 检查某个元素在页面上是否存在实例代码
2016/10/27 Javascript
Bootstrap Modal遮罩弹出层(完整版)
2016/11/21 Javascript
react.js 翻页插件实例代码
2017/01/19 Javascript
jquery.validate.js 多个相同name的处理方式
2017/07/10 jQuery
javascript读取本地文件和目录方法详解
2020/08/06 Javascript
Vue中正确使用Element-UI组件的方法实例
2020/10/13 Javascript
[02:25]DOTA2英雄基础教程 虚空假面
2014/01/02 DOTA
python 列表,数组和矩阵sum的用法及区别介绍
2018/06/28 Python
python实现将一维列表转换为多维列表(numpy+reshape)
2019/11/29 Python
利用OpenCV和Python实现查找图片差异
2019/12/19 Python
Python计算公交发车时间的完整代码
2020/02/12 Python
教你如何用python操作摄像头以及对视频流的处理
2020/10/12 Python
德国最大的网上鞋店之一:Schuhe24.de
2017/06/10 全球购物
激光脱毛、蓝光和护肤:Tria Beauty
2019/03/28 全球购物
超市开学活动方案
2014/03/01 职场文书
怎么写好自荐书
2014/03/02 职场文书
村干部承诺书
2014/03/28 职场文书
5.12护士节演讲稿
2014/04/30 职场文书
党员活动日总结
2014/05/05 职场文书
公司捐款倡议书
2014/05/14 职场文书
医院义诊活动总结
2014/07/04 职场文书
离婚协议书格式
2015/01/26 职场文书
上课迟到检讨书范文
2015/05/06 职场文书
放飞理想主题班会
2015/08/14 职场文书
MySQL中distinct和count(*)的使用方法比较
2021/05/26 MySQL
python中super()函数的理解与基本使用
2021/08/30 Python