python推导式全局变量多参数传参装饰器
创始人
2024-02-22 23:47:30
0

 

 

目录

一、推导式运用

 二、全局变量

 三、多参数传参

 四、装饰器


一、推导式运用

# 推导式# for i in range(10):
#     print(i)# 创建列表 其中奇数位为1, 偶数位为0a=[ 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 valval=2hello()
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+breturn  innerprint(outer(1))# 传入1,2等值得到结果3f=outer(1)print(f(2))print(outer(1)(2))

 

 

 案例:日志输出到控制台

from functools import wrapsdef 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:# 现在将⽇志打到指定的logfileopened_file.write(log_string + '\n')return wrapped_functionreturn decorator@logging()
def hello():passhello()

 

# 案例:编写一个记录函数执行时间的装饰器
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_wrapperreturn 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("=============================")

 


相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...