黑松山资源网 Design By www.paidiu.com
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 [] '''
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 炉石传说最强战士卡组是哪个 战士最强天梯卡组推荐一览
- LexVandyke《HistoriadeunAmor情陷地中海》[WAV+CUE]
- TheBestPopClassics《LIGHTRHYTHMSOUND2》[WAV+CUE]
- TheLatinSoundofLexVandyke-ConciertodeAranjuez拉丁情人2012[SACD-ISO]
- 《怪物猎人世界》史低¥44,《耻辱终极版》仅需¥12
- 杉果秋促最后一天!海量3A大作击穿Steam史低
- 【果娘聊天室】国庆假期就这么水灵灵地结束啦!
- 《死或生》恋爱游戏新角色:来自大雷温柔公主的喂食
- 《寂静岭2》原制作人盛赞《寂静岭2:重制版》:新玩家能玩 我超高兴
- 艾什莉体模家今日份的COS:《鸣潮》守岸人 雪白香肩清纯而诱惑
- 微信积分商城进入方法,微信积分商城介绍
- 希诺宁进不进常驻说明,希诺宁角色分析
- 【原神】V5.0攻略 | 大型开图世界任务整合
- 群星.1990-细听锋芒(2014华星40金碟复刻版)【华星】【WAV+CUE】
- 郭婷筠.2024-爱你到底为啥物【福茂】【FLAC分轨】