+ 5
"""
belal bayrakdar
you need two lambdas
"""
add10 = lambda func: lambda *args,**kwargs: func(*args,**kwargs)+10
@add10
def add5(y):
return y+5
@add10
def sub3(y):
return y-3
print(add5(5))
print(add5(15))
print(sub3(6))
caps = lambda func: (lambda *args,**kwargs: func(*args,**kwargs).upper())
@caps
def greet(name):
return f'Hello, {name}!'
print(greet('jack'))
@caps
def farewell(name='bill'):
return f'goobye, {name}'
print(farewell('wick'))
print(farewell())
print(farewell(name='kill'))
+ 6
There is no practical difference between a function declared with 'def' keyword and a lambda. Either of them can take another function as input.
But I am really not sure what you mean by "lambda decorator", I haven't heard of such a thing. Defining a decorator has a specific syntax, it wraps around another function and returns a closure. I imagine writing all this inside a lambda expression. Even if it's possible, it might destroy the readability...
+ 5
Maybe this is what you meant:
https://stackoverflow.com/questions/4125893/how-to-apply-decorators-to-lambdas?noredirect=1&lq=1
+ 4
Sololearn's python is only 3.8
import sys
print(sys.version)
for version 3.9 above, you can put the lambda expression directly after the @ symbol. It doesn't work here.đ
@lambda func: lambda *args,**kwargs: func(*args,**kwargs)+10
def add5(y):
return y+5
+ 2
decorators need a wrapper function inside.
#regular form:
def add10(func):
def wrap(*args,kwargs):
func(*args,**kwargs) + 10
return wrap
#lambda form:
add10= lambda func: lambda *args,**kwargs: func(*args, **kwargs) + 10
+ 2
it's not, just some random thing to know, I guess.đ