zoukankan      html  css  js  c++  java
  • python的高级特性3:神奇的__call__与返回函数

    __call__是一个很神奇的特性,只要某个类型中有__call__方法,,我们可以把这个类型的对象当作函数来使用。

    也许说的比较抽象,举个例子就会明白。

    In [107]: f = abs
    
    In [108]: f(-10)
    Out[108]: 10
    
    In [109]: dir(f)
    Out[109]: 
    ['__call__',
     '__class__',
     '__delattr__',
     '__dir__',
    ...]

    上例中的f对象指向了abs类型,由于f对象中有__call__方法,因此f(-10)实现了对abs(-10)的重载。

    ps:由于变量/对象/实例可以指向函数,而函数能够接受变量,因此可以看出函数可以接受另一个函数作为参数,所以__call__就实现装饰器的基础。

    扩展部分:返回函数

    函数或类一般有返回值,而python中有一个神奇的特性就是返回函数

    In [134]: %cpaste
    Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
    :
    :def lazy_sum(*args):
    :    def sum():
    :        ax = 0
    :        for n in args:
    :            ax = ax + n
    :        return ax
    :    return sum
    :--
    
    In [135]: f = lazy_sum(1,3,5,7,9)
    
    In [136]: f
    Out[136]: <function __main__.lazy_sum.<locals>.sum>
    
    In [137]: f()
    Out[137]: 25

    为什么返回函数能够这么神奇,咱们一探究竟。

    In [138]: dir(f)
    Out[138]: 
    ['__annotations__',
     '__call__',
     '__class__',
     ...
     '__getattribute__',
    ...
     '__setattr__',
    ]

    查看一下type,真相打败,原来是因为f里有__call__的内建方法。

  • 相关阅读:
    PostGIS常用SQL
    PostGIS相关数据类型及内置函数介绍
    基于GeoTools做GeoJson,PostGIS,Shapefile的转换
    如何使用GeoTools
    JavaScript 循环
    jquery 层级选择器
    jquery 表单选择
    js 字符串操作
    js对象
    js函数 作用域 闭包 回调
  • 原文地址:https://www.cnblogs.com/alexkn/p/4659921.html
Copyright © 2011-2022 走看看