4 ms·
Does anyone else find Python just as 'crazy' as Javascript gets called?
by spraak 10y ago
Does anyone else find Python just as 'crazy' as Javascript gets called?
- lmm 10y agoNo. Python is probably the least crazy mainstream language IMO. (Except the middle-endian for comprehensions, and the silly truthiness definition for dates)
- spraak 10y agoI find it weird that there are built-in functions like `map` but string objects still have methods like `split` - but I am so used to everything being a method on an object in JS. I mean that it feels so much nicer to do: let x = [ 1, 2, 3].map(i => i * 10) than x = list(map((lambda i: i * 10), [ 1, 2, 3 ]))
- lmm 10y agoThere's a lot of value in having the option of using top-level functions e.g. len. Sometimes something is more natural as a method, sometimes it's more natural as a function. In the specific case I probably agree with you, but I don't think any of the differences qualify as "crazy"; generators being lazy by default is a reasonable choice (though I think the wong one), map is often used partially applied so it's nice to have it available as a function, => was only recently added to javascript and I would hope Python will get something similar sooner or later (though I'd rather have scala-style _).
- spraak 10y agoAll good points, thanks for explaining!
- otakucode 10y agoWhat does the _ do in Scala? I've not used Scala, and it wasn't terribly clear from context unless you mean they use _ exactly as javascript uses => (which would seem really weird to me so I'm assuming its different, apologies if that is asinine).
- lmm 10y ago"_" is kind of the equivalent of "x => x" (only rather than x it's some temporary generated name that doesn't overlap with anything else). So you'd write "_ * 10" rather than "i => i * 10"
- Nullabillity 10y ago_ is a placeholder for the next lambda argument. So, for example, `x => x.blah()` could be rewritten as `_.blah()`, and `x => blah(x)` could be rewritten as `blah(_)` (or, since we don't need partial application here, simply `blah`). It also works for multiple arguments: `_ * _` is equivalent to `(x, y) => x * y`, which is really `(x, y) => x.*(y)`.
- ufmace 10y agoI read that apparently it is impossible to add a true lambda functionality to Python because of the whitespace blocks thing. There's intentionally no way to block multiple statements together except to have them on multiple lines sharing an indent level, which means there's no way to write a lambda longer than a single expression and have a syntax for it that makes sense.
- lmm 10y agoThat issue has never really bothered me tbh. If you're writing in functional style then everything tends to be a single expression anyway, and in the rare cases where you need statements the function tends to be complex enough to be worth naming IMO.
- lyschoening 10y agoThe pythonic (i.e. right) way to write this is: x = [i * 10 for i in [1, 2, 3]]
- SeanDav 10y agoi. let x = [ 1, 2, 3].map(i => i * 10) ii. x = list(map((lambda i: i * 10), [ 1, 2, 3 ])) iii. x = [i * 10 for i in [1, 2, 3]] All these methods are really just multiple sides of the same coin. All I see is different syntax, none looks any better from my point of view and unless there is a clear performance benefit, just use what you are used to, or what your coding standards define.
- NegativeLatency 10y agoI think in general the comprehension is faster because of how method calls and symbol lookups work in python. For the most part it's just a style thing.
- lyschoening 10y agoii. vs iii. boils down to explicit rather than implicit. iii. is simply easier to understand. I believe most people would also agree that ii. doesn't look particularly nice.
- spraak 10y agoI understand superficially that this works, but can you explain the syntax (or is there a name for this)? So far I've also seen a for loop like for i in [1,2,3]: ...
- spraak 10y agoAnswer to my question: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions https://docs.python.org/2/tutorial/datastructures.html#list-...
- deleted 10y ago[deleted]
- Sean1708 10y ago>>> 1 + '1' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' No.
- crazedpsyc 10y ago>>> float('inf') < 'hi' True Fixed in Python 3, but still. There are more of these things.
- teaearlgraycold 10y agoSo, you mean it was fixed 8 years ago, then.
- bradbeattie 10y agohttps://docs.google.com/spreadsheets/d/1VgQgL6Hh0JTqMqNCedEohLJfgFSgMBZpSQtcj_FkzpQ/edit?usp=drivesdk https://docs.google.com/spreadsheets/d/1VgQgL6Hh0JTqMqNCedEo...