python - Decorators: passing variables from the deepest level to the top level decorator -
i'm facing strange problem request help. here details:-
while understanding decorators , applying them in work, have scenario:-
def f3(funca, **kw): ## decorates f2 , acts prerequisite f2 work ## code setup pre-conditions ## requestobj = getrequestobj() kw['requestobj']=requestobj def inner(funcb, **k): k.update(kw) ## ## return funca(funcb, **k) return inner def f2(func, **kw): # decorates f1 , acts prerequisite f1 work kw['serverobject']=kw['requestobj'].getserverobject() def inner(**k): k.update(kw) return func(**k) return inner f2=f3(f2) def f1(**kw): ##do on requestobj , serverobj in kw f1=f2(f1)
here, understand f1 , f2 decorated functions. and, things working till now. now, have requirement in function f3
, should calling code setup pre-conditions
optionally based on parameter should passed through call f1
i.e. f1
decides whether code setup pre-conditions
in f3 runs or not. ps: need call f1
like: f1(setup_pre_conditions=true)
my solution: think of putting these functions in class , setting flag
variable on fly true
or false
, using flag variable in f3
decide whether code setup pre-conditions
should called or not. but, using method, i'll have create new object of class everytime.
edit thought of solution. may can guide me if correct!
lets put code in inner functions, have full control can reach top lowest level of chained decorators
is there better way can this?
Comments
Post a Comment