Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is __init__ not called after __new__ SOMETIMES
    primarykey
    data
    text
    <p>Let me start with this is not a repeat of <a href="https://stackoverflow.com/questions/6903355/why-does-init-not-get-called-if-new-called-with-no-args">Why does __init__ not get called if __new__ called with no args</a>. I have tried to carefully construct some sample code for <code>__new__</code> and <code>__init__</code> that has no explanation I can find.</p> <p>The basic parameters:</p> <ul> <li>There is a base class called NotMine as it comes from another library (I'll disclose at the end, not important here)</li> <li>That class has an <code>__init__</code> method that in turn calls a <code>_parse</code> method</li> <li>I need to override the <code>_parse</code> method in subclasses</li> <li>which subclass I'm creating is not known until invocation</li> <li>I know there are factory design methods but I cannot use them here (More at the end)</li> <li>I have tried to make careful use of <code>super</code> to avoid the problems in <a href="https://stackoverflow.com/questions/6032053/python-logging-why-is-init-called-twice">Python logging: Why is __init__ called twice?</a></li> <li>I know this is also 'kind of' an AbstractBaseMehtod opportunity but that did not help</li> </ul> <p>Anyway, <code>__init__</code> should be called after <code>__new__</code> 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.</p> <pre><code>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 </code></pre> <p>If you execute the code, you can see that for each call to <code>__new__</code> it announces "which door" it is exiting through and with what type. I can exit the same "door" with the same "type" object and have <code>__init__</code> called in one case and not the other. 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 <code>__init__</code> called.</p> <p><strong>End Notes:</strong> The <code>NotMine</code> library I'm using is the <a href="http://genshi.edgewall.org/" rel="nofollow noreferrer">Genshi</a> MarkupTemplate and the reason for not using a Factory design method is that their TemplateLoader needs a defaultClass to construct. I don't know until I start parsing, which I do in <code>__new__</code>. There is a lot of cool voodoo magic that genshi loaders and templates do that make this worth the effort. </p> <p>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. Things are working well but this unexplained behavior is an almost certain bug later. </p> <p><strong>UPDATE:</strong> Ignacio, nailed the top line question, if the returned object is not an "instance of" <em>cls</em> then <code>__init__</code> is not called. I do find that calling the "constructor" (e.g. <code>AA(args..)</code> is wrong as it will call <code>__new__</code> again and you are right back where you started. You could modify an arg to take a different path. That just means you call <code>ABC.__new__</code> twice rather than infinitely. A working solution is to edit <code>class ABC</code> above as:</p> <pre><code>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 </code></pre> <p>Notice the last few lines. Not calling <code>__init__</code> 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 <code>__init__</code>. I don't like the above edit but least I get the rules a little better now.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload