+ 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'))
22nd Oct 2022, 2:06 PM
Bob_Li
Bob_Li - avatar
+ 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...
22nd Oct 2022, 1:19 PM
Tibor Santa
Tibor Santa - avatar
22nd Oct 2022, 1:22 PM
Tibor Santa
Tibor Santa - avatar
+ 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
22nd Oct 2022, 3:48 PM
Bob_Li
Bob_Li - avatar
+ 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
22nd Oct 2022, 2:12 PM
Bob_Li
Bob_Li - avatar
+ 2
it's not, just some random thing to know, I guess.😁
22nd Oct 2022, 4:41 PM
Bob_Li
Bob_Li - avatar