鱼C论坛

 找回密码
 立即注册
查看: 2180|回复: 2

__call__魔法方法

[复制链接]
发表于 2016-5-22 21:13:49 | 显示全部楼层 |阅读模式
10鱼币
快没有鱼币了,所以。。。。
以下为正文。。。。。
import time
class timeslong():
    def __init__(self,func):
        self.k = func
    def __call__(self):
        print(22222222222)
        start = time.clock()
        print("It's time starting ! ")
        self.k()
        print("It's time ending ! ")
        end = time.clock()
        return "It's used : %s ." % (end - start)

@timeslong
def f():
    print(11111)
    y = 0
    for i in range(10):
        y = y + i + 1
        print(y)
    return y

print(f())
这是小甲鱼关于修饰器的介绍。
那么下面有一个问题。。。
输出的结果为:
22222222222
It's time starting !
11111
1
3
6
10
15
21
28
36
45
55
It's time ending !
It's used : 0.06483575203686207 .
也就是说 运行完__init__后直接就运行了__call__魔法方法
但是网上看到的如下

Python中__call__用法实例
2015/7/31 3:22:31来源:本网整理 编辑:章玉评论:发表评论字号: S M L
这篇文章主要介绍了Python中__call__用法,需要的朋友可以参考下

本文实例讲述了Python中__call__的用法,分享给大家供大家参考之用。具体方法如下:
先来看看如下示例代码:
#call.py 一个class被载入的情况下。
class Next:
  List = []
  
  def __init__(self,low,high) :
    for Num in range(low,high) :
      self.List.append(Num ** 2)
  
  def __call__(self,Nu):
    return self.List[Nu]

如果 这样使用:
b = Next(1,7)
print b.List
print b(2)

那么回馈很正常:
[1, 4, 9, 16, 25, 36]
9
这是是在给实例对象赋值的时候才进行的对call的调用
为什么小甲鱼上面的直接调用__call__了?
求大神解释

最佳答案

查看完整内容

你的__call__(self)中没有参数自动会调用timeslong.call(); 你也可以直接调用b(2)等,会打印'c',代码和图都给你自己试试吧。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-5-22 21:13:50 | 显示全部楼层
你的__call__(self)中没有参数自动会调用timeslong.call();
  1. #__init__是初始化,self是一个指针,指代当前的类或者实例化后当前这个实例对象,和javascript
  2. #中的this或者php中的$this一样。__call__是你要调用的对象,__call__(self)指调用当前的类。
  3. #你的自定义函数f()只是修饰器中参数的func在另一地方或者位置定义而已,
  4. #程序执行会去执行@timeslong类,参数func就是f()。其实理解和内嵌函数差不多。

  5. #我写一下__call__,__add__,__sub__,__str__,__getattr__,__repr__,其他魔法方法类似。
  6. import time

  7. def fff():
  8.     print(1111)
  9.     y = 0
  10.     for i in [100,200,300,400,500]:
  11.         y += i + 1
  12.         print(y)
  13.     return y
  14.    
  15. class timeslong():
  16.     def __init__(self,func):
  17.         self.a = func
  18.         print(self.a)
  19.     def __new__(cls,func):
  20.         print('Nstr 的')
  21.         start = time.clock()
  22.         print('Time start!')
  23.         func()
  24.         print('Time ending')
  25.         end = time.clock()
  26.         print('Used:%s.'% (end - start))
  27.         return Nstr(func)

  28. class Nstr():
  29.     def __init__(self,func,numlist = ['a','b','c']):
  30.         self.nstr = func()
  31.         self.change(self.nstr)
  32.         self.numlist = numlist
  33.     def __call__(self,num):
  34.         return self.numlist[num]

  35.     def change(self,value):
  36.         if isinstance(value,str):
  37.             for i in value:
  38.                 self.nstr += ord(i)
  39.         elif isinstance(value,(int,float)):
  40.             self.nstr = value
  41.         return self.nstr

  42.     def __str__(self):
  43.         return '你好,等于' + str(self.nstr)
  44.    
  45.     def __add__(self,other):
  46.         if type(other) == type(self):
  47.             return self.nstr+other.nstr
  48.         else:
  49.             return self.nstr+other

  50.     def __radd__(self,other):
  51.         if type(other) == type(self):
  52.             return other.__add__(self)
  53.         else:
  54.             return other + self.nstr

  55.     def __sub__(self,other):
  56.         if type(other) == type(self):
  57.             return other.nstr - self.nstr
  58.         else:
  59.             return other - self.nstr

  60.     def __rsub__(self,other):
  61.         if type(other) == type(self):
  62.             return other.__sub__(self)
  63.         else:
  64.             return other - self.nstr

  65.     def __mul__(self,other):
  66.         if type(other) == type(self):
  67.             return self.nstr*other.nstr
  68.         else:
  69.             return self.nstr*other

  70.     def __rmul__(self,other):
  71.         if type(other) == type(self):
  72.             return other.__mul__(self)
  73.         else:
  74.             return other * self.nstr

  75.     def __truediv__(self,other):
  76.         if type(other) == type(self):
  77.             return self.nstr/other.nstr
  78.         else:
  79.             return self.nstr/other

  80.     def __rtruediv__(self,other):
  81.         if type(other) == type(self):
  82.             return other.__truediv__(self)
  83.         else:
  84.             return other / self.nstr

  85.     def __floordiv__(self,other):
  86.         if type(other) == type(self):
  87.             return self.nstr//other.nstr
  88.         else:
  89.             return self.nstr//other

  90.     def __rfloordiv__(self,other):
  91.         if type(other) == type(self):
  92.             return other.__floordiv__(self)
  93.         else:
  94.             return '你好,我没有定义噢'
  95.   
  96. if __name__ == '__main__':
  97.     @timeslong
  98.     def f():
  99.         print(1111)
  100.         y = 0
  101.         for i in range (10):
  102.             y += i + 1
  103.             print(y)
  104.         return y
  105.     b = f
复制代码

python魔法方法效果.jpg
你也可以直接调用b(2)等,会打印'c',代码和图都给你自己试试吧。

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
竟无语凝噎 + 5 + 5 + 3 这么给力的答案。十分感谢

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-5-25 23:56:34 | 显示全部楼层
学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 04:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表