# stacking_decorators

## STACKING DECORATORS

Lets look at decorators again. They're related to what we call "function composition" in that the decorator "eats" what's defined just below it, and returns a proxy with the same name, likewise a callable.

Here's a decorator that tells the identity function under it what letter to tack on (concatenate) to the end of string s, the argument.

The decorator itself takes an argument. The callable it then returns, the adder function, is ready to do the work of finally "eating" ident and extending what it does by the one letter).

``````

In :

def plus(char):
"""
returns a prepped adder to eat the target, and to
build a little lambda that does the job.
"""
return lambda s: f(s) + char

@plus('R')
def ident(s):
return s

ident('X')  # do the job!

``````
``````

Out:

'XR'

``````

Now let's stack up some decorators, showing how each swallows the result below. Work from the bottom up...

``````

In :

# append from the bottom up, successive wrappings
@plus('W')
@plus('A')
@plus('R')
def ident(s):
return s

print("ident('X')  :", ident('X'))
print("ident('WAR'):", ident('WAR'))

``````
``````

ident('X')  : XRAW
ident('WAR'): WARRAW

``````

Now let's bring Compose into the mix, a decorator class makes our proxies able to compose with one another by means of multiplication. Even powering has been implemented. We're free to make our target functions composable, in addition to controlling what letters to add.

``````

In :

class Compose:
"""
make function composible with multiply
also make self powerable e.g. f ** 3 == f * f * f
From: https://repl.it/@kurner
"""

def __init__(self, f):
self.func = f

def __mul__(self, other):
return Compose(lambda x: self(other(x)))

def __pow__(self, n):
if n == 0:
return Compose(lambda x: x) # identity function
if n == 1:
return self
if n > 1:
me = self
for _ in range(n-1):
me *= self # me times me times me...
return me

def __call__(self, x): # callable instances
return self.func(x)

``````
``````

In :

@Compose
@plus('W')
@plus('A')
@plus('R')
def ident(s):
return s

H = ident ** 3
H('X')

``````
``````

Out:

'XRAWRAWRAW'

``````
``````

In :

@Compose
@plus('T')
@plus('Y')
@plus('P')
def F(s):
return s

@Compose
@plus('N')
@plus('O')
@plus('H')
def G(s):
return s

H = F * G * G * F
H('')

``````
``````

Out:

'PYTHONHONPYT'

``````
``````

In :

@plus('EXPERIMENT!')
@plus('ANOTHER ')
@plus('DO ')
@plus('LETS ')
def ident(s): return s

ident('')

``````
``````

Out:

'LETS DO ANOTHER EXPERIMENT!'

``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.7251407,"math_prob":0.883962,"size":2228,"snap":"2021-31-2021-39","text_gpt3_token_len":644,"char_repetition_ratio":0.16501799,"word_repetition_ratio":0.01907357,"special_character_ratio":0.32405746,"punctuation_ratio":0.15368421,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9735591,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-02T15:43:16Z\",\"WARC-Record-ID\":\"<urn:uuid:690bbd1e-5f29-4c03-84cd-cb7b95576b81>\",\"Content-Length\":\"36925\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:82b390a5-66d0-40e6-ac68-8accc2d53eb3>\",\"WARC-Concurrent-To\":\"<urn:uuid:76773ac6-555a-4ec8-95c0-b199779935b8>\",\"WARC-IP-Address\":\"216.239.36.21\",\"WARC-Target-URI\":\"https://notebook.community/4dsolutions/Python5/stacking_decorators\",\"WARC-Payload-Digest\":\"sha1:TKCMB2MEGYZUUAHIJCSVB6QZ2R6QAI3E\",\"WARC-Block-Digest\":\"sha1:QGU36EDIPI4KDRQI7JA23V27EZABTA46\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046154321.31_warc_CC-MAIN-20210802141221-20210802171221-00153.warc.gz\"}"}