5 ms·
> there's no acceptable way to pass a function body to another in Python. What. Why would you ever want a function body as a string? What kind of vile sorcery
by dagenleg 9y ago
> there's no acceptable way to pass a function body to another in Python.
What. Why would you ever want a function body as a string? What kind of vile sorcery are you doing? One can use inspect.getsourcelines in python if really pressed to, but I find it horrifying that javascript developers actually seem to require this functionality often.
- rhodysurf 9y agoI cant figure out what he means either since he follows it up talking about map() and reduce() whcih you can use with function pointers (references? IDK what they actually are in python)
- dagenleg 9y agoI edited my post, as after giving it some thought I concluded that the author wants to pass around the source code of the function. That indeed is something that python cannot provide in cases when function is already precompiled.
- deleted 9y ago[deleted]
- zeugmasyllepsis 9y agoI'm guessing, since the author used the arrow shorthand notation in Javascript as an example, that the limitations of Python's lambdas are the chief complaint
- dagenleg 9y agoIt's more of a feature than an arbitrary limitation I think. Python philosophy, 'explicit over implicit', and that stuff.
- zeugmasyllepsis 9y agoAn intentional limitation, then :P I tend to agree with the Python philosophy, that it's usually better to extract and name a function that to declare it inline. The `asyncio` module and `await` keywords go a long way in eliminating most use cases where I'd still prefer an anonymous function, for arbitrary callbacks.
- saurik 9y agoThe author did not say "as a string" and I am extremly confused as to how you read it like that... what? Essentially, the complaint (which is extremely common) is that functions that contain statements are not expressions in Python, so functional coding styles using things like .map() and .reduce() end up fragmented (and you are horribly tempted to play extremely insane tricks with decorators to build something that feels like a Frankenstein Ruby block); this is why the author then brings up arrow functions: it has nothing to do with strings.
- dagenleg 9y agoWell truth be told it was hard for me to understand what the author meant. Thanks for clarification.
- deleted 9y ago[deleted]
- aaronchall 9y ago> the complaint (which is extremely common) is that functions that contain statements are not expressions in Python, so functional coding styles using things like .map() and .reduce() end up fragmented You mean these critics find >>> map(lambda x: x**2, filter(lambda x: not x % 2, range(11))) preferable to the "fragmented": def square(x): return x ** 2 def is_even(x): return not x % 2 >>> (square(x) for x in range(11) if is_even(x)) Both result in the same lazy iteration (to see results below - requires materialization with a constructor like list or tuple), but I find the "fragmented" approach much more readable (so long as the functions do what their name says they do - I have seen Python where they actually did the opposite...) >>> list((square(x) for x in range(11) if is_even(x))) [0, 4, 16, 36, 64, 100] >>> list(map(lambda x: x**2, filter(lambda x: not x % 2, range(11)))) [0, 4, 16, 36, 64, 100]
- hinkley 9y agoProgrammers are terrible at naming things and we copy each other's mistakes. In the real world you filter water to get the water. In most programming languages filter() is more like a strainer or colander. My guess is you ran into someone stubbornly insisting on the meaning of the word filter despite all evidence.
- kronos29296 9y agoHigher order functions maybe? Though python has them. Spread operator can be done as unpacking a list. Destructuring assignment can also be done. Map is already there while reduce is in functools stdlib. So are all the other functional stuff. Async is in python 3. Can't say what python lacked except being able use same language on front end and back end. Except if he still uses python 2 which hasn't any new features in years.