5 ms·
> In more sane languages, the expression [] means give me an empty list value, nothing more and nothing less. So then whatever the syntax for [[]]*4 is in such
by magnio 3y ago
> In more sane languages, the expression [] means give me an empty list value, nothing more and nothing less. So then whatever the syntax for [[]]*4 is in such sane languages, it gives you four empty list values.
I don't buy this. Either the syntax to give you four empty list values is some kind of comprehension, which Python has and does exactly what you like, or it must have lazy evaluation somehow. Otherwise, how can the language know you don't want to copy the existing reference but create a new empty list? If [[]]*4 were to create new list on the fly, then `[print('hello')]*4` would have to print hello 4 times, i.e., the print statement is lazily executed, which does not fit into Python at all.
- bmitc 3y agoMy question is: what's a real-world example where this [...]*n syntax buys you something that can't be done simply otherwise and that is worth the complexity tradeoff of people often using it subtly wrong?
- maxcoder4 3y agoIt's syntax sugar. I use it all the time, but of course it could always be respected by, i don't know, a for loop. It also makes writing duck typed code easier, as X3 will work for strings, bytes and lists. Overall it's a very useful feature and I don't think it's that surprising. It's pretty obvious [x]4 will reuse the same reference. Just like manually adding "x" four times will.
- mapreduce 3y ago> that is worth the complexity tradeoff of people often using it subtly wrong? I don't think that makes the complexity tradeoff worth it. This is the kind of tradeoff that makes the language implementation, reference and semantics more and more complex. Unfortunately Python has a lot of such complexities already. We don't need more of it. Such complexity hurts the creation of completing implementations. IMHO programming language specifications and implementations should be simple and consistent so that competing implementations can be developed easily. > people often using it subtly wrong? I think that... you know... before writing serious software, these people need to roll up their sleeves and just learn the damn language. Just learn what references are and how they work in Python. It's not that hard. It is most definitely easier than adding a sphagetti monster to the compiler make it bend to the will of programmers who can't be bothered to read the tutorial. I mean this gotcha about references is taught in the 4th chapter of the tutorial. Can't developers even bother to RTFM these days before developing serious software with a programming language?
- bmitc 3y ago> these people need to roll up their sleeves and just learn the damn language You really have no choice but to do that. But the critique here is that some languages make this hard. And some languages, like Python, appear deceptively simple and consistent, when they are anything but. And as I pointed out, these decisions were not really required or designed to solve certain problems. They just kind of came about in Python's development, one historically and intentionally ignorant of pre-existing languages and their good ideas. When everybody just says "learn the language" or "there's list comprehensions" or "there's for loops", then why does the [...]*n syntax exist? What problems is it solving that require the confusion that it generates?
- mapreduce 3y ago> What problems is it solving that require the confusion that it generates? I think I answered that already. It keeps the language spec consistent and simpler. Imagine the complexity you have to add to the language spec to say that when we write [] we deal with the reference to this list except in the multiplication syntax a = [[]] * 5 where the inner [] is not a reference to the list but the list value! Such special case will make the language both inconsistent and harder to understand for experienced programmers.
- bmitc 3y agoI'm asking what makes introducing the multiplication syntax worth it in the first place.
- mapreduce 3y agoAh! Misunderstood your original comment. My apologies! Yes, I am with you when you question the usefulness of the multiplication syntax. I prefer simplicity and consistency in a programming language grammar, syntax and semantics as I advocated above. So yes I'd be happy to lose that multiplication syntax. It is not worth it.
- kerkeslager 3y agoThis feels like a legitimate critique to me. I'm developing a programming language myself and I'm not going to include a syntax like this--multiplying an array by a number feels like a type error to me.
- aeonik 3y agoI wouldn't recommend just throwing the idea away, it just needs to be approached with care. You can do it in a type safe way. Research Rank Polymorphism and Array Languages: https://en.m.wikipedia.org/wiki/Array_programming https://en.m.wikipedia.org/wiki/Array_programming https://arxiv.org/abs/1907.00509 https://arxiv.org/abs/1907.00509
- kerkeslager 3y agoI mean, is there anything there that couldn't just be a library function? Not everything has to be an operator.
- aeonik 3y agoPhilosophically, I don't know what "must" or "has" to be an operator. I certainly don't want to tell you how to design your language. I'm just trying to say, don't rule the pattern out because it's perceived as a type error; you may have other reasons that are good, but my citations provide a basis of how to make the typing work (if you were inclined). If you have other reasons, even if it's just aesthetic, that's fine.
- hughesjj 3y agoTotally agreed @ just not implementing this part. I think the motivation was being able to do scalar multiplication with a vector, but it gets ugly when operating on anything but numbers imo
- hughesjj 3y agoIt's useful for math when doing scalar multiplication but imo it would make more sense to use a tuple instead of list