Why is __init__ not called after __new__ SOMETIMES

爷,独闯天下 提交于 2019-11-29 06:30:36
Ignacio Vazquez-Abrams

From the documentation:

If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.

This is to allow __new__() to return a new instance of a different class, which has its own __init__() to be called instead. You will need to detect if you're creating a new cls, and call the appropriate constructor instead if not.

Just my two cents here, but why don't you use Python duck typing to provide Genshi with something that behaves like a class ?

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.

I think it would be easier to mock a class with a factory function returning the actual created instance.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!