Python 闭包 与 类 的简单对比

Python 闭包

def outer(x):
    def inner(y):
        return x+y
    return inner


f1 = outer(100)
print(f1(10))
print(f1(20))
print(f1(30))

f2 = outer(200)
print(f2(10))
print(f2(20))
print(f2(30))

 

Python类

class Out(object):
    def __init__(self, x):
        self.x = x

    def inner(self, y):
        return self.x + y


f3 = Out(100)
print(f3.inner(10))
print(f3.inner(20))
print(f3.inner(30))

f4 = Out(100)
print(f4.inner(10))
print(f4.inner(20))
print(f4.inner(30))

 

posted @ 2020-09-22 10:10  肖伯特  阅读(283)  评论(0编辑  收藏  举报