当前位置: 首页> 游戏> 网游 > 在线购物商城_新闻播报最新_舆情监测软件免费版_太原百度快速排名提升

在线购物商城_新闻播报最新_舆情监测软件免费版_太原百度快速排名提升

时间:2025/7/10 1:56:44来源:https://blog.csdn.net/m0_51945115/article/details/143376161 浏览次数:0次
在线购物商城_新闻播报最新_舆情监测软件免费版_太原百度快速排名提升

lambda表达式

lambda表达式是一种简化的函数表现形式,也叫匿名函数,可以存在函数名也可以不存在。

使用一行代码就可以表示一个函数:

# 格式
lambda arg[参数] : exp[表现形式]
# 无参写法
lambda : "hello"
# 一般写法
lambda a,b,c : a+b+c

1.配合map()内置函数

map():用于可迭代对象中每个元素所指定的函数,返回新的迭代器

书写格式:

# 返回参数为`list`
list = map(function[函数],iterable[可迭代对象])

使用示例:

# 普通使用
strings = ["hello","world","computer"]
demo = map(str.upper,strings)
print(list(demo))
# 配合lambda使用 切割字符串
demo2 = map(lambda x: x[2:], strings)
print(list(demo2))

在这里插入图片描述

2.配合Filter()函数使用

filter():用于筛选数组中满足条件的值,并返回一个新的可迭代对象。

语法格式:

# 返回参数为list
list = filter(function[判断函数],itrable[可迭代对象])

配合lambda函数使用示例:

number =[1,2,3,4,5,6,7]
pos = filter(lambda x :x>3,number)
print(pos)

在这里插入图片描述

3.配合reduce()函数使用

这个函数不作为function中的函数,是在functools模块中的一个函数。

reduce():具体功能是作用于对一个迭代对象的元素进行积累(累加,累乘)等操作。

语法格式:

from functools import reduce
# 返回值是一个数字
reduce(function[函数] , iterable[可迭代对象], [value][可选对象,运行函数时的初始值])

使用示例:

from functools import reduce
value = reduce(lambda x, y: x * y, [1, 2, 3, 4, 5])
print(value)

在这里插入图片描述

python装饰器

decorators高级功能,允许动态修改函数或类的行为。

接收一个函数作为参数,返回一个新的函数或修改原来的函数。

wraps:在Python的装饰器模式中,@wraps(func) 是一个来自 functools 模块的装饰器,它的作用是更新被包装函数(即被装饰的函数)的一些元数据,使其在被装饰后仍然保留原有的函数名、文档字符串、注解等属性。

语法格式:

# 这个是一个注释器
@decorators_name
# 内置装饰器
# 静态方法
@staicmethod 
@类方法
@classmethod

语法格式:

def decorat_function(orange_function):def web(*arg):befor_call_code() # 执行函数前添加result = orange_function(*arg)after_call_code() # 执行函数后添加# 使用装饰器
@decorators_function
def target_function(arg1,arg2):pass

使用示例:

def time_decorator(func):@wraps(func)def wrapper(*args, **kwargs):start_time = time.time()result = func(*args, **kwargs) #获得函数的名称,以及函数参数end_time = time.time()print(f"Function {func.__name__} executed in {end_time - start_time:.4f} seconds")return resultreturn wrapper@time_decorator#使用装饰器
def my_function():print("Hello, World!")time.sleep(1)  # 模拟一些耗时的操作
# 调用函数
my_function()

在这里插入图片描述

使用示例[带参数的接收器]:

# 将参数传递给后续使用的函数
def repeat_decorator(n):  def decorator(func):  @wraps(func)  def wrapper(*args, **kwargs):  for _ in range(n):  result = func(*args, **kwargs)  return result  return wrapper  return decorator  @repeat_decorator(3)  #使用装饰器
def say_hello():  print("Hello!")  # 调用函数  
say_hello()

在这里插入图片描述

使用示例[类装饰器]:

def add_method_decorator(cls):  # 动态地为类添加一个新方法  def new_method(self):  print("This is a new method added by the decorator.")  # 使用setattr将新方法添加到类中  setattr(cls, 'new_method', new_method.__get__(None, cls))  # 返回修改后的类  return cls  # 使用@语法应用类装饰器  
@add_method_decorator  
class MyClass:  def existing_method(self):  print("This is an existing method.")  # 创建类的实例  
obj = MyClass()  # 调用原始方法  
obj.existing_method()  # 输出: This is an existing method.  # 调用由装饰器添加的新方法  
obj.new_method()       # 输出: This is a new method added by the decorator.

在这里插入图片描述

关键字:在线购物商城_新闻播报最新_舆情监测软件免费版_太原百度快速排名提升

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: