但是出现问题,注销最后的执行语句仍有输出,原因在于@login的调用,即@login相当于执行了tv = login(tv) 所以才有输出。
def login(func): print("in Login") return func@logindef tv(name): print("{name} in TV".format(name = name))#tv = login(tv)#tv('Jack')# out:# in Login如下调整可解决
def login(func): def inner(arg): print("in Login") # return func func(arg) return inner@logindef tv(name): print("{name} in TV".format(name = name))#tv = login(tv)tv('Jack')# out:# in Login# Jack in TV简单的递归函数
#!/usr/bin/env python#递归函数def calc(num): print("Number:",num) if num/2 > 1: calc(num/2) print("After Number:",num/2)calc(10)# Number: 10# Number: 5.0# Number: 2.5# Number: 1.25# After Number: 1.25# After Number: 2.5# After Number: 5.0递归实现斐波那契数列
# Fibonacci sequence# F[n]=F[n-1]+F[n-2](n>=2,F[0]=1,F[1]=1)# 斐波那契数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...fibList = [1,1]def getFib(fibList): print(fibList) if fibList[-1] + fibList[-2] < 300: fibList.append(fibList[-1] + fibList[-2]) getFib(fibList) pass passgetFib(fibList)print("[FINAL]:",fibList)# [1, 1]# [1, 1, 2]# [1, 1, 2, 3]# [1, 1, 2, 3, 5]# [1, 1, 2, 3, 5, 8]# [1, 1, 2, 3, 5, 8, 13]# [1, 1, 2, 3, 5, 8, 13, 21]# [1, 1, 2, 3, 5, 8, 13, 21, 34]# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]# [FINAL]: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]新闻热点
疑难解答