python中__call__内置函数用法实例
作者:MaxOmnis 时间:2022-06-04 11:46:32
本文实例讲述了python中__call__内置函数的用法。分享给大家供大家参考。具体分析如下:
对象通过提供__call__(slef, [,*args [,**kwargs]])方法可以模拟函数的行为,如果一个对象x提供了该方法,就可以像函数一样使用它,也就是说x(arg1, arg2...) 等同于调用x.__call__(self, arg1, arg2)。模拟函数的对象可以用于创建仿函数(functor) 或代理(proxy)
class DistanceForm(object):
def __init__(self, origin):
self.origin = origin
print "origin :"+str(origin)
def __call__(self, x):
print "x :"+str(x)
p = DistanceForm(100)
p(2000)
输出:
>>>
origin :100
x :2000
希望本文所述对大家的Python程序设计有所帮助。
标签:python,call,内置函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python subprocess库六个实例快速掌握
2021-02-22 05:53:56
Pytest实现setup和teardown的详细使用详解
2023-09-12 02:06:48
![](https://img.aspxhome.com/file/2023/6/78596_0s.jpg)
python中slice参数过长的处理方法及实例
2023-07-13 13:12:10
python 利用pywifi模块实现连接网络破解wifi密码实时监控网络
2023-06-15 11:21:03
Python中的 ansible 动态Inventory 脚本
2022-10-23 07:53:08
![](https://img.aspxhome.com/file/2023/8/65028_0s.png)
由浅入深学习TensorFlow MNIST 数据集
2021-06-04 23:57:52
![](https://img.aspxhome.com/file/2023/0/84130_0s.png)
PYQT5开启多个线程和窗口,多线程与多窗口的交互实例
2023-07-19 04:21:21
![](https://img.aspxhome.com/file/2023/6/62576_0s.jpg)
python matplotlib库绘制条形图练习题
2023-04-20 05:49:51
![](https://img.aspxhome.com/file/2023/8/97258_0s.png)
解决python3读取Python2存储的pickle文件问题
2023-03-18 14:20:30
python re正则匹配网页中图片url地址的方法
2023-06-15 20:44:20
![](https://img.aspxhome.com/file/2023/4/95234_0s.jpg)
Python实现查询某个目录下修改时间最新的文件示例
2021-07-08 00:31:18
asp如何将RGB颜色转化成到16进制的?
2009-11-26 20:41:00
![](https://img.aspxhome.com/file/UploadPic/200911/26/rgb-57s.gif)
python使用pil生成缩略图的方法
2022-06-07 13:07:44
Python异常之常见的Bug类型解决方法
2022-09-02 15:17:14
python第三方库visdom的使用入门教程
2021-12-08 22:32:51
![](https://img.aspxhome.com/file/2023/9/82719_0s.png)
python中的txt文件转换为XML
2021-12-05 10:45:48
![](https://img.aspxhome.com/file/2023/0/67310_0s.png)
Pycharm使用之设置代码字体大小和颜色主题的教程
2021-09-07 00:27:14
![](https://img.aspxhome.com/file/2023/1/93861_0s.jpg)
关于ASP代码的加密
2007-10-15 12:30:00
JavaScript实现简单贪吃蛇效果
2023-08-13 05:48:08
![](https://img.aspxhome.com/file/2023/6/56036_0s.gif)
利用Python实现读取Word表格计算汇总并写入Excel
2021-06-24 06:15:47
![](https://img.aspxhome.com/file/2023/0/93460_0s.jpg)