4 ms·
I'm not sure what you mean by escapes, but you can use parentheses as block delimiters, "and" and "or" as a semicolon, and ternary operator for conditionals
by progval 7y ago
I'm not sure what you mean by escapes, but you can use parentheses as block delimiters, "and" and "or" as a semicolon, and ternary operator for conditionals
>>> f = lambda x: (
... (
... print('a') or
... print('b')
... ) if x % 2
... else (
... print('c') or
... print('d')
... )
... )
>>> f(42)
c
d
>>> f(43)
a
b
- samatman 7y agoMy God, man. My eyes! That is strikingly hideous. After such knowledge, what forgiveness?
- saagarjha 7y agoA language that actually cares about functional programming.
- cle 7y agoOh please. You can "do" functional programming just fine in Python. If it's longer than a line, give it a name. It's not hard and it makes the code more readable and easier to maintain.
- saagarjha 7y agoI do most of my work in multi-paradigm languages–so it's not like they're specifically designed around the ergonomics of working with functional programming–but Python is probably the worst language that I have used that could reasonably say that it supports this style of work. It's like it actively tries to make things strange and annoying to convince you to use other constructs (such as list comprehensions). Why can't I have a multiline lambda (…some things have no need for a name)? Why are all the functional methods free functions that take the list as the second parameter? Why was reduce arbitrarily excised from the list of built-in functions and put inside functools? Why can't I use a member function or operators as parameters without hacks like operators.__{operator_thats_a_reserved_word}__? Why do all the Python 3 functions return generators by default, when Python never really cared much about performance in the first place? It's just a death by a thousand cuts that always makes me regret even trying to do functional programming, and deep down I actually think the Python authors just don't want you do be doing it because they think it would lead to what they believe is difficult-to-understand code. Honestly, I think even C++ has better functional programming constructs than Python, even with its ugly-looking lambdas and lack of chaining, because <algorithm>'s extensiveness makes up for some of the language's warts.
- amelius 7y agoIn defense of Python, at least it supports closures that easily refer to variables higher up in the stack, whereas in C++ you always have to think about the lifetime of variables. Your other points I agree with.
- AnimalMuppet 7y agoYeah... um... if the problem was Python's lambda syntax, this doesn't look like the solution.
- emmelaich 7y agoYou can use a comma instead of `and` or `or`. Which looks a bit better.
- eru 7y agoYou can still only have one statement per lambda. And lots of Python's constructs are unfortunately statements and not expressions. The walrus operator moves Python closer to the expressions-camp. In practice, the bigger problem with a more functional Python is that the ubiquitous dicts don't have nice and concise operators for persistent operations. Eg you can concatenate two lists via +, but dicts are more cumbersome. Though that improved a bit, {dictA, dictB} mostly solves that problem. But more operators would be useful. And, of course, lack of tail call elimination puts a damper on things. You can mostly work around that problem, but eg implementing state machines as mutually recursive functions is going to be a pain.