python有趣的函数_Python中有趣在__call__函数
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
[]
'''
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:php中文网
python有趣的函数_Python中有趣在__call__函数相关推荐
- python nums函数_Python中的内置函数
Python中有很多内置函数,不需要我们调用模块可以直接使用,而且都是常用函数的封装,下面我们来看看Python中的内置函数都有那些. 1.abs() abs()是绝对值函数,把一个负数转化为正数,数 ...
- python list join函数_Python中join()函数多种操作代码实例
这篇文章主要介绍了Python中join()函数多种操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Python中有.join()和os ...
- python中len和range函数_Python中len()和range()函数
函数:len() 1:作用:返回字符串.列表.字典.元组等长度 2:语法:len(str) 3:参数: str:要计算的字符串.列表.字典.元组等 4:返回值:字符串.列表.字典.元组等元素的长度 5 ...
- python有趣函数_python中有趣的函数
filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决 ...
- python中如何截取字符串函数_python中如何截取字符串函数_python截取指定字符串_Python 字符串操作(string...
在python有各种各样的string操作函数.在历史上string类在python中经历了一段轮回的历史.在最开始的时候,python有一个专门的string的module,要使用string的方法 ...
- python中locals函数_python中locals和globals函数
首先,是关于名字空间的一个名词解释.是枯燥,但是很重要,所以要耐心些.Python使用叫做名字空间的东西来记录变量的轨迹.名字空间只是一个 字典,它的键字就是变量名,字典的值就是那些变量的值.实际上, ...
- python字典zip函数_python中如何使用zip函数将列表合并为字典?
python的作用很强大,列表和字典是两个不同类型的代码格式,虽然列表不能直接转换为字典,但是可以通过zip函数将列表合并为字典,实现列表转换为字典的需求.本文介绍zip函数实现列表合并为字典的原理和 ...
- python中execute函数_Python 中的内置函数与模块
封面图片来源:沙沙野 内容概览 内置函数(二) 模块概念 内置函数(二) ord():输入字符找该字符编码 (Unicode) 的位置 print 2. chr():输入位置数字找出其对应的字符 pr ...
- python字符串三种常用的方法或函数_python中字符串常用的函数
s ="hello world" s[2] >>> "l" 索引 s.index("l") >>> 2 ...
最新文章
- 基于REST的MVC架构设计与实现
- spring 依赖注入
- css3 动画 火箭,CSS3 火箭发射动画 寓意创新起航
- java中qq中拉伸的文件,delphi中如何实现QQ中的截图并实现拉伸放大移动的功能
- DOF和MultiSwitch的使用
- linux用u盘上传文件,linux如何挂载U盘和文件系统(或需要用到).doc
- ARP***防范与解决方案
- 面试精讲之面试考点及大厂真题 - 分布式专栏 20 降级组件Hystrix的功能特性
- sqlite3使用sqlite2创建的数据库
- CocoaPods停在Analyzing dependencies解决方案
- dd for windows
- mysql中的join问题:mysql不支持full join
- ArcGIS 后台服务器抛出异常
- 云服务器温控系统,服务器cpu温度监控软件
- html+css实现京东、英雄联盟静态页面
- 常用应用层协议及HTTP协议
- WXSS-WXML-WXS语法
- 【AI简报20210611期】Arm v9架构来啦、开源实战教你实现文字识别!
- homework-08
- 1、第一课---小玉买玩具