简体   繁体   English

为什么sympy覆盖`__new__`而不是`__init__`?

[英]Why does sympy override `__new__` instead of `__init__`?

Every object in sympy is a subclass of the Basic class, and they all use __new__ without __init__ , and mostly it's something like sympy中的每个对象都是Basic类的子类,它们都使用__new__而不使用__init__ ,并且大多数情况是这样的

def __new__(cls, some, parameter, **others):
    obj = parentclass.__new__(cls, **others)
    obj.some = some
    obj.parameter = parameter
    return obj

What's the difference to using __init__ like 像这样使用__init__什么区别

def __init__(self, some, parameter, **others):
    parentclass.__init__(self, **others)  # or super().__init__(...)
    self.some = some
    self.parameter = parameter

?

Have a look at Number . 看一下Number They want the class of the object to be flexible. 他们希望对象的类灵活。 Number(...) => Int/Float/... which can not be achieved by __init__ . Number(...) => Int/Float/... __init__无法实现。

Furthermore the __init__ would get the arguments of __new__ but you do not need the original arguments, see matexpr.py or you need them to be adapted to what __new__ already did (for example for __reduce__ ). 此外, __init__将获得__new__的参数,但是您不需要原始参数,请参见matexpr.py,或者您需要将它们调整为适用于__new__已经做过的事情(例如__reduce__ )。

Most object define their own __slots__ so there are fixed attributes that can be assigned to them. 大多数对象定义了自己的__slots__因此可以为它们分配固定的属性。 Assignment can be done in __new__ and __init__ . 可以在__new____init__进行分配。 I do not see the need to open a new __init__ for just setting them and doing no other operations - As Martijn Pieters and user4815162342 [source] pointed out the objects are immutable. 我看不到只需要设置一个新的__init__就可以进行设置,而无需执行其他任何操作-正如Martijn Pieters和user4815162342 [source]指出的,这些对象是不可变的。

Sometimes __init__ is called not, once or twice if you change the class: 有时__init__被调用,如果您更改类,则一次或两次:

class X(object):
    def __new__(self): # sorry but self is the class I apologize!
        obj = object.__new__(Y)
        return obj
    def __init__(self):
        print 1

>>> class Y(object):
    def __init__(self):
        print 2
>>> X() # no __init__ call, limiting you to stay in the class hierarchy
<__main__.Y object at 0x7f287e769350>
>>> class Y(X):
    def __init__(self):
        print 2


>>> X() # one __init__ call
2
<__main__.Y object at 0x7f287e7693d0>
>>> class X(object):
    def __new__(self):
        obj = Y()
        return obj
    def __init__(self):
        print 1


>>> class Y(X):
    def __new__(self):
        return object.__new__(self)
    def __init__(self):
        print 2


>>> X() # __init__ called twice, structure copied from number.py
2
2
<__main__.Y object at 0x7f287e7692d0>

Correct me if I am wrong. 如果我错了,请纠正我。 I do not think this answer is complete but these are complications I found worth motivating to not use __init__ additionally to that the objects should be immutable as mentioned by Martijn Pieters and user4815162342 [source] 我不认为这个答案是完整的,但是这些都是我发现值得鼓励的原因,除了Martijn Pieters和user4815162342提到的对象应该是不可变的以外,我不鼓励使用__init__ [源]

Waiting for 2 downvotes to delete the answer. 等待2次降票删除答案。

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

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