<i id='rIwG3'><tr id='rIwG3'><dt id='rIwG3'><q id='rIwG3'><span id='rIwG3'><b id='rIwG3'><form id='rIwG3'><ins id='rIwG3'></ins><ul id='rIwG3'></ul><sub id='rIwG3'></sub></form><legend id='rIwG3'></legend><bdo id='rIwG3'><pre id='rIwG3'><center id='rIwG3'></center></pre></bdo></b><th id='rIwG3'></th></span></q></dt></tr></i><div id='rIwG3'><tfoot id='rIwG3'></tfoot><dl id='rIwG3'><fieldset id='rIwG3'></fieldset></dl></div>

      <tfoot id='rIwG3'></tfoot>

      <small id='rIwG3'></small><noframes id='rIwG3'>

    1. <legend id='rIwG3'><style id='rIwG3'><dir id='rIwG3'><q id='rIwG3'></q></dir></style></legend>
        <bdo id='rIwG3'></bdo><ul id='rIwG3'></ul>

      TypeError:Object.__new__()只接受一个参数(要实例化的类型)

      TypeError: object.__new__() takes exactly one argument (the type to instantiate)(TypeError:Object.__new__()只接受一个参数(要实例化的类型))
    2. <tfoot id='oVGjD'></tfoot>

      <small id='oVGjD'></small><noframes id='oVGjD'>

    3. <i id='oVGjD'><tr id='oVGjD'><dt id='oVGjD'><q id='oVGjD'><span id='oVGjD'><b id='oVGjD'><form id='oVGjD'><ins id='oVGjD'></ins><ul id='oVGjD'></ul><sub id='oVGjD'></sub></form><legend id='oVGjD'></legend><bdo id='oVGjD'><pre id='oVGjD'><center id='oVGjD'></center></pre></bdo></b><th id='oVGjD'></th></span></q></dt></tr></i><div id='oVGjD'><tfoot id='oVGjD'></tfoot><dl id='oVGjD'><fieldset id='oVGjD'></fieldset></dl></div>
        <tbody id='oVGjD'></tbody>
      • <legend id='oVGjD'><style id='oVGjD'><dir id='oVGjD'><q id='oVGjD'></q></dir></style></legend>

        • <bdo id='oVGjD'></bdo><ul id='oVGjD'></ul>

              • 本文介绍了TypeError:Object.__new__()只接受一个参数(要实例化的类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想实现名为MyClass的类。 此类应该是单例的,并且它必须从BaseClass继承。

                最后我想出了以下解决方案:

                import random
                
                
                class Singleton(object):
                    _instances = {}
                
                    def __new__(cls, *args, **kwargs):
                        if cls not in cls._instances:
                            cls._instances[cls] = super(Singleton, cls).__new__(cls, *args, **kwargs)
                        return cls._instances[cls]
                
                
                class BaseClass(object):
                    def __init__(self, data):
                        self.value = random.random()
                        self.data = data
                
                    def asfaa(self):
                        pass
                
                
                class MyClass(BaseClass, Singleton):
                    def __init__(self, data=3):
                        super().__init__(data)
                        self.a = random.random()
                
                
                inst = MyClass(3)
                

                如果MyClass的def __init__(self, data=3)没有任何参数,则Evrythig工作正常。
                否则我会收到错误

                line 9, in __new__
                cls._instances[cls] = super(Singleton, cls).__new__(cls, *args, **kwargs)
                TypeError: object.__new__() takes exactly one argument (the type to instantiate)
                

                如何向MyClass提供任何参数?

                推荐答案

                因此,您的错误是TypeError: object.__new__() takes exactly one argument (the type to instantiate)。如果您查看您的代码,您正在执行super(Singleton, cls).__new__(cls, *args, **kwargs)super(Singleton, cls)引用object类,因为您的Singleton类正在继承object。您只需更改此设置:

                cls._instances[cls] = super(Singleton, cls).__new__(cls, *args, **kwargs)

                至此:

                cls._instances[cls] = super(Singleton, cls).__new__(cls)

                因为object不接受任何其他参数。

                这篇关于TypeError:Object.__new__()只接受一个参数(要实例化的类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
                  <tbody id='E8yuC'></tbody>
                <i id='E8yuC'><tr id='E8yuC'><dt id='E8yuC'><q id='E8yuC'><span id='E8yuC'><b id='E8yuC'><form id='E8yuC'><ins id='E8yuC'></ins><ul id='E8yuC'></ul><sub id='E8yuC'></sub></form><legend id='E8yuC'></legend><bdo id='E8yuC'><pre id='E8yuC'><center id='E8yuC'></center></pre></bdo></b><th id='E8yuC'></th></span></q></dt></tr></i><div id='E8yuC'><tfoot id='E8yuC'></tfoot><dl id='E8yuC'><fieldset id='E8yuC'></fieldset></dl></div>

                <small id='E8yuC'></small><noframes id='E8yuC'>

                • <tfoot id='E8yuC'></tfoot>

                    • <bdo id='E8yuC'></bdo><ul id='E8yuC'></ul>

                        1. <legend id='E8yuC'><style id='E8yuC'><dir id='E8yuC'><q id='E8yuC'></q></dir></style></legend>