Skip to content

Latest commit

 

History

History

python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Python

几种简单装饰器的实现

# 一 简单使用
def dec(func):
    def _dec():
        print "before func"
        func()
        print "after func"
    return _dec

# 二 装饰带有参数的函数

def dec(func):
    def _dec(a,b):
        print "bofore func(a,b)"
        func(a,b)
        print "after func(a,b)"
    return _dec

# 三 装饰带有不确定参数的函数 

def dec(func):
    def _dec(*args, **kwargs):
        print "bofore func(*args, **kwargs)"
        func(*args, **kwargs)
        print "after func(*args, **kwargs)"
    return _dec

# 四 装饰器本身带有参数,python web框架通常会看到类似这种@get('/test')
def dec(arg):
    def _dec(func):
        def __dec():
            print arg
            func()
        return __dec
    return _dec

for else语句,for执行完之后如果没跳出才执行else

for i in [1,2,3]:
    if i == 2:
        break
else:
    print("because break happens,i will never run")

用Python输出一个小于num的Fibonacci数列

a,b = 0, 1
while b<num:
    print (b),
    a, b = b, a+b