Python 函数 VI
Posted xingchenck
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 函数 VI相关的知识,希望对你有一定的参考价值。
1.今日内容大纲
- 带参数的装饰器
- 多个装饰器装饰一个函数
- 递归函数
2.具体内容
带参数的装饰器
def wrapper_out(n): def wrapper(f): def inner(*args,**kwargs): username = input('用户名:').strip() password = input('密码:').strip() with open (n)as f : for line in f: user,pwd = line.strip().split('|') if username == user and password == pwd: print('登录成功') ret = f(*args,**kwargs) return ret print('用户名或密码错误') return inner return wrapper @wrapper_out('qq') def qq(): print('成功访问qq') @wrapper_out('dy') def dy(): print('成功访问抖音') qq() dy()
多个装饰器装饰一个函数
def wrapper1(func1): def inner1(): print('wrapper1 before func')#2 func1()#3 print('wrapper1 after func')#4 return inner1 def wrapper2(func2): def inner2(): print('wrapper2 before func') #1 func2() print('wrapper2 after func')#5 return inner2 @wrapper2 #f = wrapper2(f) f内 = inner1;f外 = inner2 @wrapper1 #f = wrapper1(f) f内 = f(); f外 = inner1 def f(): print(666)#3 f()
递归函数
def age(n): if n == 1: return 18 else: return age(n-1)+2 ret = age(8) print(ret) l2 = [1, 3, 5, ['太白','元宝', 34, [33, 55, [11,33]]], [77, 88],66] def func(l): for i in l: if type(i) == list: func(i) else: print(i) func(l2) def func(n): if n == 1: h = 1 elif n == 2: h = 1 else: h = func(n-1)+func(n-2) return h func(6)
以上是关于Python 函数 VI的主要内容,如果未能解决你的问题,请参考以下文章