__call__

Python中有一个有趣的语法,只要定义类型的时候,实现__call__函数,这个类型就成为可调用的。

换句话说,我们可以把这个类型的对象当作函数来使用,相当于 重载了括号运算符。

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

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

  1. class Animal(object):
  2. def __init__(self, name, legs):
  3. self.name = name
  4. self.legs = legs
  5. self.stomach = []
  6. def __call__(self,food):
  7. self.stomach.append(food)
  8. def poop(self):
  9. if len(self.stomach) > 0:
  10. return self.stomach.pop(0)
  11. def __str__(self):
  12. return 'A animal named %s' % (self.name)
  13. cow = Animal('king', 4)  #We make a cow
  14. dog = Animal('flopp', 4) #We can make many animals
  15. print 'We have 2 animales a cow name %s and dog named %s,both have %s legs' % (cow.name, dog.name, cow.legs)
  16. print cow  #here __str__ metod work
  17. #We give food to cow
  18. cow('gras')
  19. print cow.stomach
  20. #We give food to dog
  21. dog('bone')
  22. dog('beef')
  23. print dog.stomach
  24. #What comes inn most come out
  25. print cow.poop()
  26. print cow.stomach  #Empty stomach
    1. '''''-->output
    2. We have 2 animales a cow name king and dog named flopp,both have 4 legs
    3. A animal named king
    4. ['gras']
    5. ['bone', 'beef']
    6. gras
    7. []
    8. '''

python __call__ 函数的更多相关文章

  1. 通过 python的 __call__ 函数与元类 实现单例模式

    简单一句话,当一个类实现__call__方法时,这个类的实例就会变成可调用对象. 直接上测试代码 class ClassA: def __call__(self, *args, **kwargs): ...

  2. Python中的__init__()和__call__()函数

    Python中的__init__()和__call__()函数 在Python的class中有一些函数往往具有特殊的意义.__init__()和__call__()就是class很有用的两类特殊的函数 ...

  3. python iter函数用法

    iter函数用法简述 Python 3中关于iter(object[, sentinel)]方法有两个参数. 使用iter(object)这种形式比较常见. iter(object, sentinel ...

  4. Python locals() 函数

    Python locals() 函数  Python 内置函数 描述 locals() 函数会以字典类型返回当前位置的全部局部变量. 对于函数, 方法, lambda 函式, 类, 以及实现了 __c ...

  5. python基础,函数,面向对象,模块练习

    ---恢复内容开始--- python基础,函数,面向对象,模块练习 1,简述python中基本数据类型中表示False的数据有哪些? #  [] {} () None 0 2,位和字节的关系? # ...

  6. python常用函数拾零

    Python常用内置函数总结: 整理过程中参考了runoob网站中python内置函数的相关知识点,特此鸣谢!! 原文地址:http://www.runoob.com/python/python-bu ...

  7. Python __call__详解

    可以调用的对象 关于 __call__ 方法,不得不先提到一个概念,就是可调用对象(callable),我们平时自定义的函数.内置函数和类都属于可调用对象,但凡是可以把一对括号()应用到某个对象身上都 ...

  8. python的函数

    函数一词起源于数学,但是在编程中的函数和数学中的有很大不同.编程中的函数式组织好的,可重复使用的,用于实现单一功能或相关联功能的代码块. 我们在学习过程中已经使用过一些python内建的函数,如pri ...

  9. python strip()函数 介绍

    python strip()函数 介绍,需要的朋友可以参考一下   函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)        删除s字符串中开头.结尾处,位于 rm删除 ...

随机推荐

  1. 【Linux】日志分析及管理

    日志的作用   用于记录系统.程序运行中发生的各种事件   eg: [root@localhost ~]# yum install -y httpd [root@localhost ~]# tail ...

  2. 快速提高谷歌浏览器(Chrome)自带下载器的网速

    之前每次下载东西都是复制好下载链接到迅雷中下载,会提高成倍网速,但是时间一长,感觉不方便,废话不多说,上干货~ 由于中国防火墙(GFW)的强大,在线下载Google浏览器的时候速度非常慢,如果只是单独 ...

  3. php composer 实现类的自动加载

    我们在开发项目中会经常用到第三方的类库插件,但是如果每次需要使用的时候都会在代码的某一处去引入,然后在实例化,这样做感觉很不方便,那么怎么实现自动加载呢,下面简单介绍使用composer实现自动加载: ...

  4. 每天一个Linux命令之mkdir

    Linux mkdir命令 mkdir [-p] filename 用于创建一个空目录 如果该目录下有相同名称的目录那么会报错 apple@apple-Pro  ~/Documents/java_d ...

  5. Leecode刷题之旅-C语言/python-389 找不同

    /* * @lc app=leetcode.cn id=389 lang=c * * [389] 找不同 * * https://leetcode-cn.com/problems/find-the-d ...

  6. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

  7. 自定义udf添加一列

    //创建得分窗口字典 var dict= new mutable.HashMap[Double, Int]() ){ dict.put(result_Score(i),i) } //自定义Udf函数 ...

  8. The Road to learn React书籍学习笔记(第二章)

    The Road to learn React书籍学习笔记(第二章) 组件的内部状态 组件的内部状态也称为局部状态,允许保存.修改和删除在组件内部的属性,使用ES6类组件可以在构造函数中初始化组件的状 ...

  9. Hadoop HA高可用集群搭建(2.7.2)

    1.集群规划: 主机名        IP                安装的软件                            执行的进程 drguo1  192.168.80.149 j ...

  10. 几个并发的术语解释——QPS,TPS,PV

    从英文全称翻译出字面意思就OK啦!  PV=page view TPS=transactions per second QPS=queries per second RPS=requests per ...