Why aren't python nested functions called closures?

后端 未结 8 823
情歌与酒
情歌与酒 2020-11-22 05:37

I have seen and used nested functions in Python, and they match the definition of a closure. So why are they called nested functions instead of closures<

8条回答
  •  庸人自扰
    2020-11-22 06:25

    I had a situation where I needed a separate but persistent name space. I used classes. I don't otherwise. Segregated but persistent names are closures.

    >>> class f2:
    ...     def __init__(self):
    ...         self.a = 0
    ...     def __call__(self, arg):
    ...         self.a += arg
    ...         return(self.a)
    ...
    >>> f=f2()
    >>> f(2)
    2
    >>> f(2)
    4
    >>> f(4)
    8
    >>> f(8)
    16
    
    # **OR**
    >>> f=f2() # **re-initialize**
    >>> f(f(f(f(2)))) # **nested**
    16
    
    # handy in list comprehensions to accumulate values
    >>> [f(i) for f in [f2()] for i in [2,2,4,8]][-1] 
    16
    

提交回复
热议问题