简体   繁体   English

为什么在__new__之后没有调用__init__

[英]Why is __init__ not called after __new__ SOMETIMES

Let me start with this is not a repeat of Why does __init__ not get called if __new__ called with no args . 让我从这开始不重复为什么__init__如果没有args调用__new__就不会被调用 I have tried to carefully construct some sample code for __new__ and __init__ that has no explanation I can find. 我试图为__new____init__仔细构建一些示例代码, __new__我找不到任何解释。

The basic parameters: 基本参数:

  • There is a base class called NotMine as it comes from another library (I'll disclose at the end, not important here) 有一个名为NotMine的基类,因为它来自另一个库(我最后会透露,这里不重要)
  • That class has an __init__ method that in turn calls a _parse method 该类有一个__init__方法,该方法又调用_parse方法
  • I need to override the _parse method in subclasses 我需要覆盖子类中的_parse方法
  • which subclass I'm creating is not known until invocation 我正在创建的子类直到调用才知道
  • I know there are factory design methods but I cannot use them here (More at the end) 我知道有工厂设计方法,但我不能在这里使用它们(更多在最后)
  • I have tried to make careful use of super to avoid the problems in Python logging: Why is __init__ called twice? 我试图小心使用super来避免Python日志记录中的问题:为什么__init__被调用两次?
  • I know this is also 'kind of' an AbstractBaseMehtod opportunity but that did not help 我知道这也是一种AbstractBaseMehtod机会,但这并没有帮助

Anyway, __init__ should be called after __new__ and for every explanation of why SOME samples below don't work I seem to be able to point to other cases that do work and rule out the explanation. 无论如何, __init__应该在__new__之后被调用,并且对于为什么下面的一些样本__new__的每一个解释我似乎能够指出其他__new__情况并排除解释。

class NotMine(object):

    def __init__(self, *args, **kwargs):
        print "NotMine __init__"
        self._parse()

    def _parse(self):
        print "NotMine _parse"

class ABC(NotMine):
    def __new__(cls,name,*args, **kwargs):
        print "-"*80
        print "Entered through the front door ABC.__new__(%s,%s,*%s,**%s)"%(cls,name,args,kwargs)
        if name == 'AA':
            obj = super(NotMine,ABC).__new__(AA,*args,**kwargs)
            print "Exiting door number 1 with an instance of: %s"%type(obj)
            return obj 
        elif name == 'BB':
            obj = super(NotMine,ABC).__new__(BB,*args,**kwargs)
            print "Exiting door number 2 with an instance of: %s"%type(obj)
            return obj
        else:
            obj = super(NotMine,ABC).__new__(cls,*args,**kwargs)
            print "Exiting door number 3 with an instance of: %s"%type(obj)
            return obj

class AA(ABC):

    def _parse(self):
       print "AA _parse"

class BB(ABC):

    def __init__(self, *args, **kw):
        print "BB_init:*%s, **%s"%(args,kw)        
        super(BB,self).__init__(self,*args,**kw)

    def _parse(self):
        print "BB _parse"

class CCC(AA):

    def _parse(self):
        print "CCCC _parse"


print("########### Starting with ABC always calls __init__ ############")
ABC("AA")            # case 1
ABC("BB")            # case 2
ABC("NOT_AA_OR_BB")  # case 3

print("########### These also all call __init__ ############")
AA("AA")           # case 4
BB("BB")           # case 5
AA("NOT_AA_OR_BB") # case 6
BB("NOT_AA_OR_BB") # case 7
CCC("ANYTHING")    # case 8

print("########### WHY DO THESE NOT CALL __init__ ############")
AA("BB")  # case 9  
BB("AA")  # case 10
CCC("BB") # case 11

If you execute the code, you can see that for each call to __new__ it announces "which door" it is exiting through and with what type. 如果你执行代码,你可以看到每次调用__new__它都会宣告它正在退出的“哪个门”和什么类型。 I can exit the same "door" with the same "type" object and have __init__ called in one case and not the other. 我可以使用相同的“类型”对象退出相同的“门”,并在一个案例中调用__init__而不是另一个案例。 I've looked at the mro of the "calling" class and that offers no insight since I can invoke that class ( or a subcass as in CCC ) and have __init__ called. 我已经查看了“调用”类的mro,因为我可以调用该类(或者像CCC中的子主体)并调用__init__ ,所以没有提供任何见解。

End Notes: The NotMine library I'm using is the Genshi MarkupTemplate and the reason for not using a Factory design method is that their TemplateLoader needs a defaultClass to construct. 完备注:NotMine我使用图书馆是元史 MarkupTemplate并没有使用厂设计方法的原因是,他们的TemplateLoader需要defaultClass建设。 I don't know until I start parsing, which I do in __new__ . 直到我开始解析才知道,我在__new__做了。 There is a lot of cool voodoo magic that genshi loaders and templates do that make this worth the effort. genshi加载器和模板有很多很酷的伏都魔法,这使得这个值得付出努力。

I can run an unmodified instance of their loader and currently everything works as long as I ONLY pass the ABC (abstract sort-of-factory) class as the default. 我可以运行一个未经修改的加载器实例,只要我只通过ABC(抽象工厂排序)类作为默认值,一切都可以正常工作。 Things are working well but this unexplained behavior is an almost certain bug later. 事情进展顺利,但这种无法解释的行为后来几乎是一定的错误。

UPDATE: Ignacio, nailed the top line question, if the returned object is not an "instance of" cls then __init__ is not called. 更新: Ignacio,固定顶线问题,如果返回的对象不是“ cls的实例”,则不调用__init__ I do find that calling the "constructor" (eg AA(args..) is wrong as it will call __new__ again and you are right back where you started. You could modify an arg to take a different path. That just means you call ABC.__new__ twice rather than infinitely. A working solution is to edit class ABC above as: 我确实发现调用“构造函数”(例如AA(args..)是错误的,因为它会再次调用__new__并且你回到你开始的地方。你可以修改一个arg以采取不同的路径。这只是意味着你调用ABC.__new__两次而不是无限制。一个有效的解决方案是将class ABC编辑为:

class ABC(NotMine):
  def __new__(cls,name,*args, **kwargs):
    print "-"*80
    print "Entered through the front door ABC.__new__(%s,%s,*%s,**%s)"%(cls,name,args,kwargs)
    if name == 'AA':
        obj = super(NotMine,ABC).__new__(AA,*args,**kwargs)
        print "Exiting door number 1 with an instance of: %s"%type(obj)
    elif name == 'BB':
        obj = super(NotMine,ABC).__new__(BB,*args,**kwargs)
        print "Exiting door number 2 with an instance of: %s"%type(obj)
    elif name == 'CCC':
        obj = super(NotMine,ABC).__new__(CCC,*args,**kwargs)
        print "Exiting door number 3 with an instance of: %s"%type(obj)
    else:
        obj = super(NotMine,ABC).__new__(cls,*args,**kwargs)
        print "Exiting door number 4 with an instance of: %s"%type(obj)
    ## Addition to decide who calls __init__  ##
    if isinstance(obj,cls):
        print "this IS an instance of %s So call your own dam __init__"%cls
        return obj
    print "this is NOT an instance of %s So __new__ will call __init__ for you"%cls
    obj.__init__(name,*args, **kwargs)
    return obj

print("########### now, these DO CALL __init__ ############")
AA("BB")  # case 9  
BB("AA")  # case 10
CCC("BB") # case 11

Notice the last few lines. 注意最后几行。 Not calling __init__ if it's a "different" class does not make sense to me, ESPECIALLY when the "different" class is still a subclass of the class calling __init__ . 不调用__init__如果它是一个“不同的”类对我来说没有意义,特别是当“不同”类仍然是调用__init__的类的子类时。 I don't like the above edit but least I get the rules a little better now. 我不喜欢上面的编辑,但现在我的规则变得更好了。

From the documentation : 文档

If __new__() does not return an instance of cls , then the new instance's __init__() method will not be invoked. 如果__new__()没有返回cls的实例,则不会调用新实例的__init__()方法。

This is to allow __new__() to return a new instance of a different class , which has its own __init__() to be called instead. 这是为了允许__new__() 返回另一个类的新实例,该实例有自己的__init__()来代替。 You will need to detect if you're creating a new cls, and call the appropriate constructor instead if not. 您需要检测是否正在创建新的cls,如果没有,则调用相应的构造函数。

Just my two cents here, but why don't you use Python duck typing to provide Genshi with something that behaves like a class ? 这只是我的两分钱,但你为什么不使用Python鸭子打字为Genshi提供类似于类的东西?

I took a quick look at Genshi source code and the only requirements that I saw on the 'class' parameter to the TemplateLoader is that it is callable with the given arguments. 我快速浏览了Genshi 源代码 ,我在TemplateLoader的'class'参数中看到的唯一要求是它可以使用给定的参数调用。

I think it would be easier to mock a class with a factory function returning the actual created instance. 我认为用工厂函数模拟一个返回实际创建实例的类会更容易。

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

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