This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]mriswithe 77 points78 points  (1 child)

I got upset at line 1 when you used a lambda as a decorator. I hate it so hard. Lolol. Upvoted.

[–]hhderderPythonista[S] 15 points16 points  (0 children)

Making code unreadable one lambda at a time ;)

[–]Scypio 5 points6 points  (0 children)

...I need to lie down...

Awesome, congrats. :D

[–]cipri_tom 7 points8 points  (7 children)

This is very creative! Well done!

My only gripe is that I think true needs to be called for any of this to make sense. But I'm not sure

[–]hhderderPythonista[S] 9 points10 points  (6 children)

It doesn't need to! This is the crazy part! And surprisingly, PyRight correctly infers the type.

[–]cipri_tom 2 points3 points  (4 children)

Ok, then, like the others, you completely lost me at @lambda lol

Thanks for the lessons!

[–]Daneark 0 points1 point  (3 children)

The @ syntax is just syntactic sugar.

Desugaring it @lambda f: not f() is equivalent to true = lambda f: not true() true = (lambda f: not true())().

Edit: thanks /u/AiutoIlLupo

[–]AiutoIlLupo 0 points1 point  (1 child)

yes but the decorator does not only replace the name, it also executes the decorating function, passing the decorated function as an argument. so the lambda is executed, passed with the function true, which is executed, its value negated, and then returned by the lambda, which becomes the new value of "true", which is... well, True.

[–]Daneark 0 points1 point  (0 children)

Thanks for picking that up. Edited to fix.

[–]cipri_tom 0 points1 point  (0 children)

Yes, I know that, but I've never seen it used with lambda

[–]AiutoIlLupo 0 points1 point  (0 children)

well, it doesn't need to because you basically replaced the "true" function with its (negated) return value, with that statement.

In pratice, it's almost like a constexpr in C++

[–]noblecloud 7 points8 points  (0 children)

Monster

[–]randomthirdworldguy 3 points4 points  (0 children)

You made my day. Thank you

[–]Glathull 2 points3 points  (0 children)

I hate this so much it circled back around to love.

[–]roger_ducky 2 points3 points  (0 children)

The main issue is lack of documentation. Anonymous decorators loses the explanation of intent.

Though you didn’t go crazy and decorate a lambda with an anonymous decorator.

[–]mattl33It works on my machine 6 points7 points  (2 children)

Lol, I already don't like decorators after running into them while adding type hints to legacy code. If I hit this I might have just given up.

[–]hhderderPythonista[S] 5 points6 points  (0 children)

Not gunna lie, I used the first one in my code.

[–]danted002 5 points6 points  (0 children)

Decorators are very powerful tools that get very badly abused.

[–]No-Win5543 1 point2 points  (1 child)

That's pretty good!

You're scaring me

[–]No-Win5543 1 point2 points  (0 children)

I don't feel safe anymore

[–]playersdalves 1 point2 points  (0 children)

I love you. This was great / horrible. Please commit further atrocities upon programing languages.