Python高级笔记(十)闭包

1. 闭包

#!/usr/bin/python
# -*- encoding=utf-8 -*-

def test(number):

    # 在函数里面再定义一个函数,并且这个函数用到外边函数的变量,那么将这个函数以及用到的一些变量称之为闭包
    def test_in(number_in):
        print("in test_in 函数,number_in is %d" %number_in)
        return number+number_in

    # 其实这里返回的就是闭包的结果
    return test_in

ret = test(20)
print(ret(30))

原文地址:https://www.cnblogs.com/douzujun/p/10834142.html