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:]asfaftrueView 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