标签:war 密码 user 一个 type wrapper NPU code with
带参数的装饰器
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)
标签:war 密码 user 一个 type wrapper NPU code with
原文地址:https://www.cnblogs.com/xiaohei-chen/p/11976962.html