简体   繁体   English

TypeError:object()不带参数 - 但仅限于Python 3

[英]TypeError: object() takes no parameters - but only in Python 3

I'm migrating some code from Python 2 to Python 3, and I am getting different behaviour. 我正在将一些代码从Python 2迁移到Python 3,并且我得到了不同的行为。 Looking through the lists of "what's changed" hasn't pointed me to any relevant differences, but presumably I've missed a big one. 查看“改变了什么”的列表并没有指出任何相关的差异,但可能我错过了一个重要的差异。

I have simplified my code as much as possible to get this 'minimal faulty program': 我尽可能地简化了我的代码以获得这个“最小错误程序”:

def decorator(Type):
    """ This is a class decorator. It replaces a class with a subclass which
    *should be* equivalent.

    The result works on Python 2.7 but not on Python 3.4. """

    class FactorySubclass(Type):
        """ This subclasses from the provided type, and overrides the __new__
            and __init__ methods, but replaces them with exact equivalents,
            so I can't see how this has any effect. """

        def __new__(cls, *args, **kwargs):
            # Simplified this code to do basically nothing.
            # If this line is removed, it works on both versions.
            return Type.__new__(cls, *args, **kwargs)

        def __init__(self, *args, **kwargs):
            # Simplified this code to do basically nothing.
            Type.__init__(self, *args, **kwargs)

    return FactorySubclass


@decorator
class ExampleClass(object):
    def __init__(self, param=3):
        print("Constructed example instance")


ec = ExampleClass(param=5)

This code runs, and prints Constructed example instance in Python 2.7. 此代码运行,并在Python 2.7中打印Constructed example instance This code fails, and dumps a stack trace in Python 3.4. 此代码失败,并在Python 3.4中转储堆栈跟踪。

Traceback (most recent call last):
  File "mfp.py", line 31, in <module>
    ec = ExampleClass(param=5)
  File "mfp.py", line 16, in __new__
    return Type.__new__(cls, *args, **kwargs)
TypeError: object() takes no parameters

Typically this error means that someone has misspelled __init__ (and so constructor parameters are bypassing the relevant class and being given to object 's parameterless constructor, but that doesn't seem to be the case here. 通常这个错误意味着某人拼错__init__ (因此构造函数参数绕过相关类并被赋予object的无参数构造函数,但这似乎不是这种情况。

Oh, and as an afterthought, I confirmed that, yes, the value of param was 5 in Python 2.7. 哦,作为事后的想法,我确认,是的,在Python 2.7中, param的值是5。

2to3 gives it a clean-bill of health. 2to3给它一个清洁的健康状况。

Please give me a pointer to a change in Python 3 that would invalidate this code, so I may read more about it. 请给我一个指向Python 3中的更改的指针,该更改将使此代码无效,因此我可能会阅读更多相关内容。

You have the answer in your question: 你有问题的答案:

Typically this error means [...] constructor parameters are [...] being given to object's parameterless constructor [...] 通常这个错误意味着[...]构造函数参数被赋予对象的无参数构造函数[...]

To fix, change your decorator to only add __init__ and __new__ if the passed-in Type has those methods. 要修复,如果传入的Type具有这些方法, __new__装饰器更改为仅添加__init____new__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM