python 学习笔记三(高阶函数)
** 高阶函数:接受函数作为参数,or 把函数作为返回结果; eg:map
from operator import mul from functools import partial triple = partial(mul, 3) # 使用 mul 创建 triple 函数,把第一个定位参数定为 3。 triple(7) #21 list(map(triple, range(1, 10))) # 在 map 中使用 triple;在这个示例中不能使用 mul。 # [3, 6, 9, 12, 15, 18, 21, 24, 27]
** 调用类:1)运行 __new__创建(构造)一个实例 2)运行 __init__ 初始化实例,返回给调用方
==》相当于调用函数;
==》覆盖 __new__ 方法的话,也可能出现其他行为
==》如果类定义了 __call__ 方法,那么它的实例可以作为函数调用
** 函数的参数【定位参数--》仅限关键字参数】
def tag(name, *content, cls=None, **attrs): """生成一个或多个HTML标签""" if cls is not None: attrs['class'] = cls if attrs: attr_str = ''.join(' %s="%s"' % (attr, value) for attr, value in sorted(attrs.items())) else: attr_str = '' if content: return '\n'.join('<%s%s>%s</%s>' % (name, attr_str, c, name) for c in content) else: return '<%s%s />' % (name, attr_str)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?