目录
一、推导式运用
二、全局变量
三、多参数传参
四、装饰器
一、推导式运用
 
 # 推导式
# for i in range(10):
#     print(i)
    # 创建列表 其中奇数位为1, 偶数位为0
a=[ i for i in range(10)]
a2=[ 1 if i %2 ==0 else 0  for i in range(10) ]
print(a)
print(a2)
print("=======================================")
# 需求:取出下面课程分数大于94的科目及具体分数
class_dict = {
    'c++': 90,
    'python': 93,
    'java': 95,
    'javascript': 96,
    'node.js': 94
}
b={ item[0] for item in class_dict.items()}
b2={item[1] for item in class_dict.items()}
print(b)
print(b2)
c={ item[0]:item[1] for item in class_dict.items()  if item[1] >94  }
print(c)
print("=======================================")
# k,v方法2
c={ k:v for k,v in class_dict.items()  if v >94  }
c2={ k:v  if v >94  else 0 for k,v  in class_dict.items()  }
print(c)
print(c2)
 
 

二、全局变量
global使用
 
 val=1
def hello():
    global val
    val=2
hello()
print(val)
# 在java代码里报错 ,在python里则相反为1 -->  global val设置全局变量
 
 

三、多参数传参
# 多参数解析 数组 def res(arg1, arg2, arg3): print(f'参数1为:{arg1}') print(f'参数2为:{arg2}') print(f'参数3为:{arg3}') # res(1,2,3) arr=(1,2,3) res(*arr) print("=======================================") def say (*args): print(args) for i in args: print(i) say(1,2,3,5,6) print("=======================================") # 字典 class_dict = { 'arg1': 90, 'arg2': 8, 'arg3': 'lucy' } res(**class_dict) print("=======================================") kwargs = {"one": 3, "two": "1", "three": 5} print(kwargs)

四、装饰器
闭包函数:声明在一个函数中的函数,叫做闭包函数。
闭包:内部函数总是可以访问其所在的外部函数中声明的参数和变量,即使在其外部函数被返回了之
后。
装饰器是闭包的一种应用。 类似于java中的AOP
装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使
# 装饰器 def outer(a): def inner(b): return a+b return inner print(outer(1)) # 传入1,2等值得到结果3 f=outer(1) print(f(2)) print(outer(1)(2))

案例:日志输出到控制台
from functools import wraps
def logging(logfile='out.txt'):
    def decorator(func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            log_string = func.__name__ + "被调用了"
            # 打开logfile,并写⼊内容
            with open(logfile, 'a') as opened_file:
                # 现在将⽇志打到指定的logfile
                opened_file.write(log_string + '\n')
        return wrapped_function
    return decorator
@logging()
def hello():
    pass
hello()

 
 # 案例:编写一个记录函数执行时间的装饰器
def time_wrapper(func):
    def wrapper():
        t1 = time.time()
        func()
        t2 = time.time()
        print(f'总耗时{t2 - t1}')
    return wrapper
@time_wrapper
def hello():
    for i in range(3):
        time.sleep(1)
hello()
print("=============================") 
 
 

def logging(level): def outer_wrapper(func): def inner_wrapper(*args, **kwargs): print(f"{level}: enter {func.__name__}()") return func(*args, **kwargs) return inner_wrapper return outer_wrapper @logging("error") def hello(): for i in range(3): print('time.sleep(1)') hello()

被装饰的方法带参数
def transaction(func):
    def wrapper(n):
        print("开启")
        func(n)
        print("关闭")
    return wrapper
@transaction
def hello(name):
    print(f"{name} say hello world")
hello("zhangsan")
print("=============================")



















