元类单例如何工作

如何解决元类单例如何工作

我不清楚 the typical metaclass singleton implementation 是如何工作的。我希望加星标的打印执行两次;它只发生一次:

class _Singleton(type):
    _instances = {}
    def __call__(cls,*args,**kwargs):
        print('Within __call__',cls,cls._instances,**kwargs)
        if cls not in cls._instances:
            print('**About to call __call__',super(_Singleton,cls).__call__,flush=True)
            print("Is cls the '...of object'?",hex(id(cls)).upper())
            cls._instances[cls] = super(_Singleton,cls).__call__(*args,**kwargs)
            print(cls._instances[cls])
        return cls._instances[cls]


class MySingleton(metaclass=_Singleton):
    pass


if __name__ == '__main__':
    print('Making mysingleton')
    mysingleton = MySingleton()
    print("Is mysingleton the 'cls'?",mysingleton)

    print('Making another')
    another = MySingleton()

    # Verify singletude
    assert another == mysingleton

这个打印

Making mysingleton
Within __call__ <class '__main__.MySingleton'> {}
**About to call __call__ <method-wrapper '__call__' of _Singleton object at 0x000001C950C28780>
Is cls the '...of object'? 0X1C950C28780
<__main__.MySingleton object at 0x000001C9513FCA30>
Is mysingleton the 'cls'? <__main__.MySingleton object at 0x000001C9513FCA30>
Making another
Within __call__ <class '__main__.MySingleton'> {<class '__main__.MySingleton'>: <__main__.MySingleton object at 0x000001C9513FCA30>}


根据我对 Python 文档的经验,它们非常循环且令人困惑。文档说 __call__() 在实例被“调用”时被调用。很公平;运行 MySingleton.__call__ 是因为 mysingleton = MySingleton() 上的“()”表示函数调用。 MySingleton 是 _Singleton 类型,所以它有一个 _instances 字典。字典在第一次调用时自然是空的。条件失败并执行 Super(_Singleton,cls).__call__

super() 在这里做什么?从文档中几乎无法理解。它返回一个“代理对象”,在别处解释为“一个'引用'共享对象的对象”,它将方法调用委托给'type'的父类或兄弟类。好的;它将用于调用一些相关的_Singleton 类型的方法。

这里使用的 super() 的两个参数形式“精确指定参数并进行适当的引用”。那些是什么参考?类型是_Singleton,对象是cls,不是mysingleton。它是任何对象 0x000001C950C28780。无论如何,super() 搜索顺序是getattr()super()。我认为这意味着根据 _Singleton.__mro__ 查找引用,因为 __call__ 不是属性(或者是?)。也就是说,super() 调用根据 super() 进行查找,我假设它是 _Singleton。清如泥。 __mro__ 产生 (<class '__main__._Singleton'>,<class 'type'>,<class 'object'>)。因此,super(_Singleton,cls) 将查找“相关的_Singleton 类型”并调用其__call__ 方法;我假设那是 cls.__call__()

由于 cls 是 _Singleton,我希望看到第二个打印。实际上,我希望有某种递归。两者都不会发生。里面发生了什么?

解决方法

内建的 super 并不是 Python 语法中最简单的东西。当一个方法在类的层次结构中被覆盖时使用,并允许间接指定哪个版本(在哪个祖先类中定义的方法)将被实际调用。

这里,_Singletontype 的子类。很公平。但是,由于 __call___Singleton 方法已被覆盖,因此必须在其父类中调用相同的方法才能实际构建对象。这就是 super(_Singleton,cls).__call__(*args,**kwargs) 的目的:它将调用转发到 _Singleton 的父级。所以它是一样的:

type.__call__(cls,*args,**kwargs)

即:它调用__call__type方法,但仍然使用cls作为self对象,允许创建{{ 1}} 对象并绕过对 MySingleton 的递归调用。替代方法是使用 _Singleton.__call__ 或直接使用 S.__new__(S,**kwargs),但最后一种会绕过任何可能的对象初始化。

实际上,object.__new__(S) 是这里的 Pythonic 方式,因为如果您以后构建更复杂的元类层次结构(_Singleton super 将确保立即使用该类位于层次结构中的 super(_Singleton,cls) 之前。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-