当前位置: 首页 > news >正文

建设百度网站海南省零售户电商网站

建设百度网站,海南省零售户电商网站,wordpress 二级页面,室内设计用什么软件比较好Register用法 1. 为什么使用Register2. 先验知识-----装饰器2.1 代码视角的装饰器用法2.2 装饰器的使用场景2 3. Registry注册器参数parent介绍未完待续 1. 为什么使用Register 2. 先验知识-----装饰器 2.1 代码视角的装饰器用法 实例1 decorate def func():pass #! 等价于…

Register用法

  • 1. 为什么使用Register
  • 2. 先验知识-----装饰器
    • 2.1 代码视角的装饰器用法
    • 2.2 装饰器的使用场景2
  • 3. Registry注册器参数parent介绍
  • 未完待续

1. 为什么使用Register

2. 先验知识-----装饰器

2.1 代码视角的装饰器用法

  • 实例1
@decorate
def func():pass
#! 等价于func = decorate(func)
  • 实例2
def a_new_decorator(a_func):def wrapTheFunction():print("I am doing some boring work before executing a_func()")a_func()print("I am doing some boring work after executing a_func()")return wrapTheFunction@a_new_decorator
def a_function_requiring_decoration():"""Hey yo! Decorate me!"""print("I am the function which needs some decoration to remove my foul smell")a_function_requiring_decoration()
# Output: I am doing some boring work before executing a_func()
#         I am the function which needs some decoration to remove my foul smell
#         I am doing some boring work after executing a_func()print(a_function_requiring_decoration.__name__)
# Output: wrapTheFunction

实际上我们希望"name"函数输出函数的名字,这样在后续代码排查的时候可以提高效率,但是这里的函数被wrapTheFunction代替了,重写了我们的函数名字和注释文档。因此在python需要借用functools.warps来解决这个问题。

  • 实例3
def a_new_decorator(a_func):@wrap(a_func)def wrapTheFunction():print("I am doing some boring work before executing a_func()")a_func()print("I am doing some boring work after executing a_func()")return wrapTheFunction@a_new_decorator
def a_function_requiring_decoration():"""Hey yo! Decorate me!"""print("I am the function which needs some decoration to remove my foul smell")a_function_requiring_decoration()
# Output: I am doing some boring work before executing a_func()
#         I am the function which needs some decoration to remove my foul smell
#         I am doing some boring work after executing a_func()print(a_function_requiring_decoration.__name__)
# Output:a_function_requiring_decoratio

2.2 装饰器的使用场景2

  • 实例1(带参数的装饰器)
from functools import wrapsdef logit(logfile='out.log'):def logging_decorator(func):@wraps(func)def wrapped_function(*args, **kwargs):log_string = func.__name__ + " was called"print(log_string)# 打开logfile,并写入内容with open(logfile, 'a') as opened_file:# 现在将日志打到指定的logfileopened_file.write(log_string + '\n')return func(*args, **kwargs)return wrapped_functionreturn logging_decorator@logit()
def myfunc1():passmyfunc1()
# Output: myfunc1 was called
# 现在一个叫做 out.log 的文件出现了,里面的内容就是上面的字符串@logit(logfile='func2.log')
def myfunc2():passmyfunc2()
# Output: myfunc2 was called
# 现在一个叫做 func2.log 的文件出现了,里面的内容就是上面的字符串

3. Registry注册器参数parent介绍

在 Python 的 “Registry”(注册器)模式中,参数 “parent” 通常表示父级对象或父级注册器。“Registry” 模式用于管理、存储和检索对象、函数或其他实体,允许不同的组件或模块向注册器中注册或注销实体,然后其他组件可以查询并使用这些实体。

当在 “Registry” 模式中使用参数 “parent” 时,通常是指在注册一个实体时,需要将其关联到某个父级对象或父级注册器。这可以用于创建层次结构、管理依赖关系或组织数据。

下面是一个示例,展示了在 “Registry” 模式中使用参数 “parent” 的可能含义:

class Registry:def __init__(self):self._registry = {}def register(self, name, item, parent=None):if parent is not None:parent.register(name, item)else:self._registry[name] = item# 使用示例
root_registry = Registry()
child_registry = Registry()root_registry.register("item_a", "Item A")
child_registry.register("item_b", "Item B", parent=root_registry)

在这个示例中,我们定义了一个 “Registry” 类,其中的 register 方法接受参数 “parent”。当注册实体时,如果指定了 “parent” 参数,那么实体将被注册到父级注册器中,否则将被注册到当前注册器中。这允许我们创建层次结构的注册器,其中子注册器可以继承父注册器中的实体。

需要注意的是,具体的含义和用法可能会根据你的需求和实现方式而有所不同。在 “Registry” 模式中,参数 “parent” 的含义取决于你如何设计和使用注册器。

未完待续

注册 构建

http://www.yayakq.cn/news/756682/

相关文章:

  • 如何为网站添加谷歌分析工具wordpress下载附件
  • 业绩显示屏 东莞网站建设技术支持表白网站制作生成器
  • php网站开发流程aspcms 网站栏目管理
  • 烟台公司网站开发北京哪些做网站的公司好
  • 做网站好的百姓装潢公司口碑
  • 西安网站排名分析wordpress链接前面的图标
  • 哪个网站建设公司比较好python 网站开发那个好
  • 相亲网站wordpress like 插件
  • dede做英文网站优化单位网站设计制作
  • 南京网站建设耐油橡胶板南昌做网站流程
  • 医疗网站建设模板制作自己做网站大概多少钱
  • 许昌那有做网站做网站 流量怎么抓钱
  • 阿里云建wordpress站检测网站开发语言
  • 网站视频做背景公司网站页面设计思路
  • 什么行业做网站多微商城手机网站制作
  • 个人主题网站河南省建设工程信息网站
  • 做网站功能的框架结构图过期网站.
  • oracle 网站开发网站建设服务器费用
  • 网站后台验证码不显示页面设计器
  • wordpress 插件分类狼雨seo网站
  • wordpress 建企业网站有创意的网络广告案例
  • 百度站长工具综合查询做网站需要的素材资料
  • 国外文本排版设计网站保定网络公司网站
  • 华为公司网站建设方案模板wordpress动态新闻代码
  • 辽宁建设培训网站怎么做公司的官方网站
  • 有哪些做动图网站西安微网站开发
  • 西安外贸建站做微新闻怎么发视频网站
  • 找网站开发项目网站的维护
  • 江西专业的企业网站建设公司wordpress更换域名重定向
  • 在门户网站管理建设工作讲话域名后缀一览表