3 ms·
[found_node.get("value") for node in tree if (found_node := node.find(SOME_XPATH)) is not None] tree.map(node => node.find(SOME_XPATH)).filter(Boolean).map(nod
by joeframbach 8y ago
[found_node.get("value") for node in tree if (found_node := node.find(SOME_XPATH)) is not None]
tree.map(node => node.find(SOME_XPATH)).filter(Boolean).map(node => node.get("value"))
I can deal with either language at this level of complexity. Anything more complicated needs more LoC in either language.
- williamdclt 8y agoI vastly prefer the second one, as I would expect to see it written something like this: tree .map(node => node.find(SOME_XPATH)) .filter(Boolean) .map(node => node.get("value")) I don't use Python on a daily basis, but I find that infinitely more readable than the comprehension version (plus, it's a syntax used in several major languages)
- lukeschlather 8y agoIn fairness, the Python also should be broken up. It should be: [ found_node.get("value") for node in tree if (found_node := node.find(SOME_XPATH)) is not None ] I think I also prefer Ruby/Javascript style comprehensions, but it's worth comparing well formatted code.
- hellofunk 8y agoWhat is "node => ", is that valid Python? I'm new to the language and have never seen the => syntax before. The docs for 'map' don't show it either.
- vthriller 8y agoIt's not Python, the second is actually a counterexample in JavaScript, where `node =>` is similar to Python's `lambda node:`
- deleted 8y ago[deleted]
- deleted 8y ago[deleted]