Python Adorner execution mechanism

Source: Internet
Author: User

Frontier:

The first is to see a singleton model, and wonder why the parameters in outer can stand like global parameters.

#single-Case modeldefSingle_model (CLS): Instance= {}    defInner (*args, * *Kwargs):Print        ifCls not inchInstance:instance[cls]= CLS (*args, * *Kwargs)Print 'CLS not in instance'        returnInstance[cls]returnInner@single_modelclassMyfoo (object):def __init__(self): Self.name='WWT'Self.age= 12MF1=Myfoo () mf2=Myfoo () mf1.name='ASFAF'PrintMf2.namePrintOfs ismf2[out:]asfaftrue
View Code

So I was very strange, but later put instance in the inner of the singleton model failed, so the problem must be on the outer of the adorner.

1 order of execution between adorner levels

defSingle_model (text):Print 'Last_outer'+textdefOuter (func): Instance= {}        Print 'Instance_outer:%d'%ID (instance)defInner (*args, * *Kwargs):ifFunc not inchInstance:instance[func]= Func (*args, * *Kwargs)Print 'Instance_inner:%d'%ID (instance)returnInstance[func]returnInnerreturnOuter@single_model ('zhhh')deffunc ():return 'Hahahha'Con1 Executing the program at this moment [Out:]last_outer Zhhhinstance_outer:31691640#that is, after the adorner is created, it executes before the Func call. -----------------------------------------------Con2 Call Function ABC=func () EFG=func ()PrintABC, Efg[out:]last_outer zhhhinstance_outer:30905208Instance_inner:30905208Hahahha Hahahha#If an object (instance) is defined in the outer or more outer layer and referenced in the inner layer, the object will not be present by GC,#similar to global

2 Decorators

Python Adorner execution mechanism

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.