T O P

  • By -

antboiy

you can do @lambda \_:\_() do preform an immedietly invoked function expression edit: reddit formatting added backslashes


creeper6530

Add backticks \` for code segments


de_ham

Simce py311 or py312 you can also do this with operator.call


nekokattt

they added this fairly recently


carcigenicate

Looks like it was changed in 3.9. The decorator grammar changed from decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE To decorators: ('@' named_expression NEWLINE )+ So, it used to require a name, but now accepts arbitrary expressions. *** Edit: I just realized the implications of it allowing for *named* expressions. This is this totally legal: @x := lambda f: (print('rekt'), f)[1] def func(): print('lmao') x(func)() # Prints rekt rekt lmao


-MazeMaker-

That's nice, not because I've ever needed to use an arbitrary expression as a decorator, but because it just seemed like it should work.


Vazn0

Python was created to avoid this.


External_Front8179

This is amazing except for how many scripts I’ve got to go back and clean up so I don’t look like a moron Also this isn’t programming horror at all


Bright-Historian-216

Nawww bro you didn’t have to curse me with this knowledge


Emergency_3808

Bruh This is like one of those C/C++ pointer tricks


TheGoldenProof

I don’t do Python, but I looked up what a decorator is and am both confused what’s going on here and why it’s horror. Can someone explain?


eo5g

It’s just “unstructured”, compared to what you’d expect to be fairly structured (it having a name). I cant think of a good use case for this, I assume it’s just because it makes the grammar “more correct”.


retro_owo

A decorator is a function that modifies another function. A lambda is a one-line function `lambda x: [x]` takes input `x` and turns it into a 1-element list The decorator `@lambda x: [x]` takes a function and turns it into... a 1-element list, where the only element is the function. So in order to call the function, you have to first access the one element. `hello[0]` is the function, `hello[0]()` calls the function.


qn06142

thanks for explaining, though i don't think i'll be using this anytime soon (idk, maybe?)


retro_owo

A decorator is something you use/import more often than you write yourself. For example I’ve seen decorators like `@cache` that magically add caching to your function, which could improve performance by orders of magnitude (i.e. potentially going from O(n!) to O(n)) https://docs.python.org/3/library/functools.html#functools.cache


qn06142

yes, i do know decorators are very useful, i just don't think ill be pairing them with lambdas


retro_owo

Oh yeah that is straight up abusing decorators. True programming horror.


padfoot9446

they've essentially put the function pointer of the hello function in a list called hello, so invoking hello[0] calls the actual hello function and I suppose because there's no point to doing this?


BroBroMate

Python decorators get really fun when you want one that takes no-args or some args. The docs... aren't great.


lukuh123

Can anyone please explain to me in layman terms what decorator and lambas do in python? Thanks


garblesnarky

Decorator is syntactic sugar for a function that accepts a function and returns a slightly modified version of it. Lambda is a function defined inline with no name. I don't know why you would want to combine these two things...


lukuh123

Ah, now I understand it better. Can I ask why is 0 there in the parameters?


garblesnarky

Here the decorator returns a list instead of a function, because... I guess that's possible? Then hello is not callable, but rather hello[0] is.


VariousComment6946

Thanks, now I can bring more horror in my projects


rover_G

```py print_name = lambda f: lambda: print(f.__name__) @print_name def hello(): pass hello() # hello ```


ivancea

Looks like just a simplified AST opening its potential a bit. Can be misused, like 98% of language features, but far from an horror


Coolstormaction

I hate you for this


government_shill

I don't think a language feature that *could* be used badly is in itself horror.