阅读背景:

【Python】指向函数的变量_小M同学的博客

来源:互联网 

这个例子比较典型,使用了指向函数的变量,有两层封装。

from mxm import container_abcs
from itertools import repeat
def _ntuple(n):
    def test_func(x):
        if isinstance(x, container_abcs.Iterable): #如果已经是转换后的值,直接返回,不需要再做转换操作
            return x
        return tuple(repeat(x, n))
    return test_func # 返回的是函数
_single = _ntuple(1)
_pair = _ntuple(2) # _pair是指向函数的变量,这里调用的_ntuple 最终是上面定义的 test_func(x)函数,_ntuple参数2传给test_func里面的n,调用_pair时指定的参数才是传递给test_func函数的x值
_triple = _ntuple(3)
_quadruple = _ntuple(4)
from mxm im



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: