Strange Pecan class

Source: Internet
Author: User

I recently obtained an ironic for a while and finally made it available for normal use. You can take a look at the ironic api release process over the past two days. ironic-api release does not adopt the lightweight framework pecan as nova-api-implements the api release process on its own. So I learned about pecan. I didn't expect the pecan document webpage to be broken down, so I had to read the source code. Here, we do not want to elaborate on the implementation structure of the pecan framework, but just want to explore the inheritance and instantiation of the Pecan class and introduce the experiment of calling _ new.

First look at the code:

Class PecanBase (object): class ExplicitPecan (PecanBase): class Pecan (PecanBase): def _ new _ (cls, * args, ** kw): if kw. get ('use _ context_locals ') is False: self = super (Pecan, cls ). _ new _ (<strong> ExplicitPecan </strong>, * args, ** kw) print 'use _ context_locals' self. _ init _ (* args, ** kw) return self return super (Pecan, cls ). _ new _ (cls)

Initialize a Pecan instance as follows:

From pecan import corep = core. Pecan ('ironic. api. controllers. root. Rootcontroller ')
There is no doubt about such initialization, and the if statement does not determine whether the result is true or not. Returns a Pecan instance.

Which of the following classes will p be used for initialization?

P = core. Pecan ('ironic. api. controllers. root. Rootcontroller', use_context_locals = False)
Here use_context_locals = False is passed in, if judgment is true, and if body is executed. However, the _ new _ function of the parent class is very special here. It imports the ExplicitPecan instead of the default cls (that is, Pecan) and returns an ExplicitPecan instance, and call its _ init _ for initialization. The _ new _ function of Pecan will return an initialized ExplicitPecan instance. If the ExplicitPecan instance does not have init_context_local (), how can it continue to call the _ init _ of Pecan?
After experiments, it is proved that the returned value p is an ExplicitPecan instance, not a Pecan instance.


Make an experiment:

Class A (object): def _ new _ (cls, * args): print "A _ new __:", cls return super (A, cls ). _ new _ (cls, * args) def _ init _ (self, * args): print "A _ init _", self super (, self ). _ init _ (* args) class B (A): def _ new _ (cls, * args): print "B _ new __:", cls return super (B, cls ). _ new _ (cls, * args) def _ init _ (self, * args): super (B, self ). _ init _ (* args) print "B _ init _", self class C (A): def _ new _ (cls, * args ): print "C _ new __:", cls return super (C, cls ). _ new _ (<strong> B </strong>, * args) def _ init _ (self, * args): super (C, self ). _ init _ (* args) print "C _ init _", self

Run the following command and result:

In [2]: B = testforInit. B () B _ new __: <class 'testforinit. B '> A _ new __: <class 'testforinit. B '> A _ init _ <testforInit. B object at 0x7fb6cdf70150> B _ init _ <testforInit. B object at 0x7fb6cdf70150> In [3]: c = testforInit. C () C _ new __: <class 'testforinit. c'> A _ new __: <class 'testforinit. B '> In [4]: print B. _ class __< class 'testforinit. B '> In [5]: print c. _ class __< class 'testforinit. B '>
Here we can see that B calls the _ new _ function of the parent class according to the conventional method, and the execution sequence and result of the function are normal. But when C calls the _ new _ function of the parent class, B is passed in, not C. The result shows that only the _ new _ functions of C and A are executed, and a B instance is returned. The _ init _ functions of A, B, and C are not called. Here, we can also explain why to call the ExplicitPecan. _ init _ function in the if of Pecan. _ new _. Otherwise, an uninitialized ExplicitPecan instance is returned.


Strange Pecan class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.