Post AkzvHTWi1debHJDwsS by tartley@mastodon.social
 (DIR) More posts by tartley@mastodon.social
 (DIR) Post #AkzL5WTncigac0reS0 by tyil@fedi.tyil.nl
       2024-08-15T10:33:05.731Z
       
       0 likes, 0 repeats
       
       Why does #Python still not have anonymous functions in 2024?
       
 (DIR) Post #AkzLllsqk3t6Y7j8bY by Suiseiseki@freesoftwareextremist.com
       2024-08-15T10:40:35.836896Z
       
       1 likes, 0 repeats
       
       @tyil I mean, C, the best language, doesn't have anonymous functions.Although GCC's implementation of GNU C offers anonymous function via a macro.
       
 (DIR) Post #AkzMAu7FGv8hJwRdLc by babakounine@eldritch.cafe
       2024-08-15T10:41:42Z
       
       0 likes, 0 repeats
       
       @tyil aren't lambda functions anonymous fonctions ?
       
 (DIR) Post #AkzMAv0tvzeu6YU568 by tyil@fedi.tyil.nl
       2024-08-15T10:45:14.441Z
       
       0 likes, 0 repeats
       
       @babakounine@eldritch.cafe They are technically, but don't allow newlines, making them useless for anything beyond extremely simple statements. I want to be able to define a proper function, but anonymously.#Perl has had it for a long while. #Javascript has them, #PHP, #Ruby (iirc). #Python is pretty much the last one of "modern" languages to not have anything serious for it. Why?
       
 (DIR) Post #AkzMET081flENroZRA by hugovk@mastodon.social
       2024-08-15T10:45:16Z
       
       0 likes, 0 repeats
       
       @babakounine @tyil Yep:lambdaAn anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is `lambda [parameters]: expression`https://docs.python.org/3/glossary.html#term-lambda
       
 (DIR) Post #AkzMETzoKL6JTAfpa4 by tyil@fedi.tyil.nl
       2024-08-15T10:45:53.926Z
       
       0 likes, 0 repeats
       
       @hugovk@mastodon.social @babakounine@eldritch.cafe Okay now let me use newlines like in a regular function.
       
 (DIR) Post #AkzN4ITeOlU4nrL11s by veronica@mastodon.online
       2024-08-15T10:52:30Z
       
       0 likes, 0 repeats
       
       @tyil @babakounine Do you actually mean just newlines, or do you mean multiple expressions? The former is definitely possible.
       
 (DIR) Post #AkzN4J2kIHYaYhl2jQ by tyil@fedi.tyil.nl
       2024-08-15T10:55:15.847Z
       
       0 likes, 0 repeats
       
       @veronica@mastodon.online @babakounine@eldritch.cafe If you're going to recommend to escape newlines with a \ because this is theoretically possible, I am very, very sorry for you.
       
 (DIR) Post #AkzNAZGIuEweb75LjU by veronica@mastodon.online
       2024-08-15T10:56:06Z
       
       1 likes, 0 repeats
       
       @tyil @babakounine I'm not. Use parenthesis, as is the convention.You did not answer my question though.
       
 (DIR) Post #AkzX9Jjo8cZfhbvahU by danthedeckie42@social.vivaldi.net
       2024-08-15T11:08:11Z
       
       0 likes, 0 repeats
       
       @tyil I think it's the "zen of python" thing, right? "Explicit is better than implicit". So anonymous functions (aka lambda) is implicit, but creating a named function and calling that is explicit. ?
       
 (DIR) Post #AkzX9KVJI0ZW4w9Wk4 by tyil@fedi.tyil.nl
       2024-08-15T12:48:10.860Z
       
       0 likes, 0 repeats
       
       @danthedeckie42@social.vivaldi.net The zen of python is a shallow lie, though. I mean, there's a regular for which is quite explicit, but "the pythonic way" is to use the much more obtuse inline style for loop.
       
 (DIR) Post #AkzXSlMReyTEsOGxii by bouncing@twit.social
       2024-08-15T11:35:46Z
       
       0 likes, 0 repeats
       
       @tyil @babakounine It’s by design. The point is to avoid unnecessary complexity.
       
 (DIR) Post #AkzXSmF2O08hbhoYoS by tyil@fedi.tyil.nl
       2024-08-15T12:51:43.684Z
       
       0 likes, 0 repeats
       
       @bouncing@twit.social That would make sense if the language was actually designed that way all around, but its really not. There's multiple way to do for loops, with the most explicit one not being the recommended one. There's a plethora of ways to format a string, all with their own neat little issues. There even are multiple ways to do functions, just not this particular one. #Python has adopted a large swath of unnecessary complexity over time, why not one that actually would make sense?
       
 (DIR) Post #Akzt1C03DovQNW3Z1k by danthedeckie42@social.vivaldi.net
       2024-08-15T16:47:30Z
       
       0 likes, 0 repeats
       
       @tyil 💯 .  it's a poem, not a law.  Using a list comp instead of a for loop is only a good idea if it makes the code cleaner and easier to understand.
       
 (DIR) Post #Akzt1Cd2sq7KKSIho8 by tyil@fedi.tyil.nl
       2024-08-15T16:53:15.189Z
       
       0 likes, 0 repeats
       
       @danthedeckie42@social.vivaldi.net Well, if its not a law, then I don't see any sane reason why they still haven't figured out how to do anonymous functions right. It seems odd that a poem should be severely restricted in how to express yourself, especially since pretty much every other (modern) language has implemented them. #Python seems a major step down coming from literally any other language.
       
 (DIR) Post #AkzvHTWi1debHJDwsS by tartley@mastodon.social
       2024-08-15T17:12:25Z
       
       0 likes, 0 repeats
       
       @tyil I think the justification is that, when someone suggests first class anonymous functions on python.ideas, or some similar forum, they are asked to provide concrete examples where it would make the code better, and the consensus of opinion is that it doesn't really seem to do that. I appreciate there are things you can do with an anonymous function, but the trade-offs seem ambiguous, at best.
       
 (DIR) Post #AkzvHUJH74VBhvwjZo by tartley@mastodon.social
       2024-08-15T17:13:05Z
       
       0 likes, 0 repeats
       
       @tyil Writing a language off as a "major step down" seems like an emotional over-reaction. All languages have features that match our personal current favorite styles, and features that don't. Part of learning a language is learning how to change our current coding style to match the language's strengths.
       
 (DIR) Post #AkzvHVhPwnPM16FWqW by tartley@mastodon.social
       2024-08-15T17:16:18Z
       
       0 likes, 0 repeats
       
       @tyil FWIW, here's a post from 2006 in reaction to the years - no, DECADES - of discussion about why first class anonymous functions in Python are not actually a good idea: https://www.artima.com/weblogs/viewpost.jsp?thread=147358
       
 (DIR) Post #Al2m0cz7I56FZmHb6W by leonerd@fosstodon.org
       2024-08-15T21:26:01Z
       
       0 likes, 0 repeats
       
       @tyil @babakounine Python doesn't have variable capture from outer scopes, or closures; thus actual anonymous functions would be far less useful than you'd imagine.Compare:```perlmy @closures;foreach my $x ( 1 .. 10 ) {  push @closures, sub { return $x };}```each of the ten closures pushed to the array captures the value $x had been when it was created. Invoking each would return its own captured value. Python's one doesn't do that.
       
 (DIR) Post #Al2m0dkcRT65x6VX96 by tyil@fedi.tyil.nl
       2024-08-17T02:18:50.859Z
       
       0 likes, 0 repeats
       
       @leonerd@fosstodon.org That's actually a fair point, I hadn't thought about that.
       
 (DIR) Post #Al2m0efh1Gkco7D76e by leonerd@fosstodon.org
       2024-08-15T21:26:28Z
       
       0 likes, 0 repeats
       
       @tyil @babakounine Actually fun fact: Go's near-equivalent didn't do that either until fairly recently