Live data from Hacker News

Why Operators Are Useful

neopythonic.blogspot.com

131–140 of 173 posts

Re: Why Operators Are Useful

#131

Earlier quoted context omitted.

> I for one feel that + is a very intuitive choice for concatenating strings and lists. Is there any other common operator you would recommend instead? Literally, any other operator would be better than addition. A space, a dot, a product, two dots, a minus sign, whatever. Anything except the visually commutative plus. > Because I feel that using a less common one would harm beginner-friendliness significantly. Usage…

“A”+”B” what do you feel should happen? “A”*6 what do you feel should happen?

> “A”+”B” what do you feel should happen?

Clearly, that should be undefined. You cannot sum two strings. Unless you are really deranged and you want it to be "C".

Re: Why Operators Are Useful

#132

I'm amazed (and horrified) how come such an intelligent mathematician and designer of a beautiful programming language made such an obvious fuckup of using a commutative operator for string concatenation. Really, I hate python just for this single idiotic notation. I mean, it's right there in front of your eyes. He talks about the convenience of using a visually commutative operator like "+" for commutative operation…

Julia does this with *. IMO it weirds people out

> Julia does this with *

A much saner choice. I would prefer a space, still.

Re: Why Operators Are Useful

#133

I'm amazed (and horrified) how come such an intelligent mathematician and designer of a beautiful programming language made such an obvious fuckup of using a commutative operator for string concatenation. Really, I hate python just for this single idiotic notation. I mean, it's right there in front of your eyes. He talks about the convenience of using a visually commutative operator like "+" for commutative operation…

I understand the confusion it may cause to a new Python developer who comes from the mathematical background. Does it cause any problems beyond that? For example, does it interfere with some elegant patterns, or result in some bug prone code, etc? FWIW, the single main annoyance I've had with python was its treatment of strings as iterables of single character strings. While not wrong in any obvious theoretical sense…

> I understand the confusion it may cause to a new Python developer who comes from the mathematical background.

I'm confused mainly because a person with a mathematical background created a language with such an obvious blunder. When I have to use the language (and I do often) I am not confused about string concatenation, I am ashamed of having to use this ridiculous notation.

Re: Why Operators Are Useful

#134
post #96

Earlier quoted context omitted.

1. Mathematicians have different priorities than programmers, and they use different tools. Working with an equation on a whiteboard, it's easier to write "a+b+c" and then cancel terms as needed. When writing a formula on my computer, cancelling terms is something I almost never do, so it would be silly to use a notation that's been optimized for that. When I am doing algebra on my computer, I hope I have a tool like…

Most programming languages do have some variant of `sum(seqence)`. Python certainly does. Or, like, loops, which do the same thing. But they're optimized for different things. Using the same tool for infinite length sequences and fixed length sequences doesn't make a whole lot of sense. We often have different types for them (tuple/record vs. list/array) too.

Having done addition in both infix and prefix varieties on my computer, over the past few decades, I don't understand why prefix notation is considered 'optimized' for indefinite (not 'infinite') sequences and infix notation is considered optimized for definite length sequences.

What exactly "doesn't make a whole lot of sense" about (+ a b)? (It doesn't look the same as you wrote it in school? Neither does "3+4j", or "math.sqrt".)

Being able to use the same tool for different types of data is precisely what makes high-level programming so powerful. As Alan Perlis said, "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." Having only one way to add numbers (in languages that do that) is a great feature.

Python's insistence on these pre-selected groupings of functionality has always made it frustrating for me to work with. The two ways to add numbers look and work nothing alike. Does "TOOWTDI" not apply to math?

(Yes, I'm also frustrated and confused that Python has built-in complex numbers, and a standard 'math' module with trigonometric functions, but math.cos(3+4j) is a TypeError. What possible benefit is there of having a full set of complex-compatible functionality, but hiding it in a different math module, with all the same method names?)

Re: Why Operators Are Useful

#135

Wow, he's still got it. I read the whole thing, without realizing it was Guido Rossum, thinking "hey, that's a really good point". Then got to the end and realized it was the inventor of the Python language. So, I wasn't impressed as I was reading it because of who it was, I was impressed because he was making a really good point. Obviously, the fact that he made my favorite programming language was no fluke.

Ramblings through technology, politics, culture and philosophy by the creator of the Python programming language. This was literally the first thing I read when I hit the link. Don't mean to sound rude but how did you miss that ? Does it render differently on web? I saw this on the mobile version of the site on my Android.

[deleted]

Re: Why Operators Are Useful

#136
post #134

Earlier quoted context omitted.

Most programming languages do have some variant of `sum(seqence)`. Python certainly does. Or, like, loops, which do the same thing. But they're optimized for different things. Using the same tool for infinite length sequences and fixed length sequences doesn't make a whole lot of sense. We often have different types for them (tuple/record vs. list/array) too.

Having done addition in both infix and prefix varieties on my computer, over the past few decades, I don't understand why prefix notation is considered 'optimized' for indefinite (not 'infinite') sequences and infix notation is considered optimized for definite length sequences. What exactly "doesn't make a whole lot of sense" about (+ a b)? (It doesn't look the same as you wrote it in school? Neither does "3+4j", or…

The zen never says TOOWTDO, it says TO(APOO)OWTDI. (That's "there's one, and preferably only one, obvious way to do it.)

`reduce(op.add, [x, y])` works. Python could remove it's infix ops and use only prefix ones. But prefix ones aren't obvious. And as Guido says, readability matters.

Re: Why Operators Are Useful

#137
post #85

Earlier quoted context omitted.

Your comment is begging the question. The reason that Lisp “+” can accept a list of arbitrary length, rather than a pair, is that the underlying addition operator is associative.

Not true. Division isn't associative and you can do e.g. (/ 12 6 3)

This is true - Lisp is making / left-associative.

But this observation does not change my point: that the parent comment is saying "Lisp already has the ability to do + on lists", but the reason "+ on lists" makes sense is because Lisp is using the underlying associativity of mathematical +. And the latter associativity property, for abstract mathematical "+", is what the blog post is describing/exploring.

Re: Why Operators Are Useful

#138

A “dict” operator would be vague because there is more than one way to combine dictionaries. Why should I have to guess whether duplicate keys are being skipped or replaced by a symbol, when two different well-named functions will always make it clear?

In Ruby, we have Hash#merge, e.g. { a: 1, b: 2 }.merge({ b: 3, c: 4 }) gives { a: 1, b: 3, c: 4 }, which is typically what you want, e.g. args.merge(reset: false) to have reset set to false in the args even if present.

That's how dict.update works in python also. The other poster is saying that they don't like the + operator having the property of using the values from the right operand to update the left operand vs the clear semantics of the method call.

Re: Why Operators Are Useful

#139

Earlier quoted context omitted.

Well that's exactly why string concat in Julia is

Indeed, Julia has the most well-thought-out commentary on strings in their docs I've seen from a non-toy language: https://docs.julialang.org/en/v1/manual/strings/#man-concate...

Elixir's string module documentation isn't half bad, but there is more practical Unicode thinking and less mathematical thinking (which makes sense, given their domains).

https://hexdocs.pm/elixir/String.html

Re: Why Operators Are Useful

#140
JavaScript deals with this well. The equivalent of the proposed

    d3 = d1 + d2
is

    let d3 = {...d1, ...d2};
In fact, it seems like you can already do this in python:

    d3 = {**d1, **d2}
This seems to have most of the benefits of being a dedicated syntax (rather than just a function call), without the downsides of breaking the commutativity of the + operator.
Post reply on HN