News for hackers. Stuff that matters.
Re:Interview Gone Wrong by reader9274 on Sunday November 03, 2024 @10:13PM (#42038094)
If you know nothing about Python or coding, and you see a=b=c, you'd think that's true when all three a,b,c are the same. Python does that beautifully here, and that's the intent. It's not Python that's confusing, it's your previous experience with other languages that's confusing you.
Re:Interview Gone Wrong by Terr_ on Sunday November 03, 2024 @10:37PM (#42038213)
Yeah, the behavior is arguably much less confusing than the alternative of:

    5==5==5   # All three the same
    (5==5)==5
    true==5
    false
Or

    1==2==false # All three different, in a troublesome way
    (1==2)==false
    false==false
    true
Re:Interview Gone Wrong by zdragnar on Sunday November 03, 2024 @11:21PM (#42038406)
I dunno, I'd expect the right hand expression (2 == false) to be resolved first, then compared to 1.

Compare this with

    a = b = c = 5
You evaluate the innermost (i.e. the tail) expression first, and work your way out to the head. As a sexpr it is a little more obvious that way:

    (= a (= b (= c 5)))
An argument could easily be made that python is making up special rules for the equality operator by treating the duplication of the operator as merging the expressions into one.

Instead of what you would expect, per the rules of literally every other expression, 5 == 5 == 5:

    (== 5 (== 5 5))
It gets rewritten as

    (== 5 5 5)
Which one is "unexpected" is really a matter of perspective: are you reading with the rules of English or the rules of your programming language as your context?

I do concede one caveat to this argument: mathematical operators tend to change operator precedence around to avoid making mathematical mistakes. I am mildly of the opinion that this itself was a mistake. Most calculators don't do it, so it's not like anyone would be* that* thrown off by it.

Re:Interview Gone Wrong by furyofantares on Monday November 04, 2024 @12:25AM (#42038750)
> An argument could easily be made that python is making up special rules for the equality operator by treating the duplication of the operator as merging the expressions into one.

I guess the argument could be made but it would be wrong. All the comparison operators can be chained in python. a < b < c, for example, or even a == b <= c < d.

Re:Interview Gone Wrong by pansa2 on Monday November 04, 2024 @02:01AM (#42039209)
> You evaluate the innermost (i.e. the tail) expression first, and work your way out to the head. As a sexpr it is a little more obvious that way: `(= a (= b (= c 5)))`

That's not true in Python though.

    a = b = c = <expression>
is equivalent to:

    temp = <expression>; a = temp; b = temp; c = temp
I don't know why that order of evaluation was chosen.
Re:Interview Gone Wrong by toomanyrichies on Sunday November 03, 2024 @10:19PM (#42038123)
> It's not Python that's confusing, it's your previous experience with other languages that's confusing you.

"There are no children here at the 4H club either! Am I so out of touch?

No... it's the children who are wrong."

-Principal Skinner, "The Simpsons"

EDIT to clarify- I'm just being silly, not suggesting anyone is right or wrong here.

Re:Interview Gone Wrong by ball_of_lint on Monday November 04, 2024 @12:50AM (#42038888)
Most programming languages ought not to be optimized for a non-programmer to read, but rather for someone who writes code to read.

There's a lot of options for a language to deal with a statement like this. It could be a syntax error, a compile or lint warning, it could work by doing order of operations and comparing the boolean from one compare with the third element, or it could work in the way you described.

I'd prefer languages that I work with to chose earlier options from that list. In most languages this sort of statement is usually a bug, and deserves explicit parenthesis to clarify the order of operations. I really don't want to have to pull out an operator precedence chart in order to read your code, much less have to look up some esoterica about this specific language.

Re:Interview Gone Wrong by the5avage on Monday November 04, 2024 @03:21AM (#42039604)
> I really don't want to have to pull out an operator precedence chart in order to read your code

Hard disagree here. I write code for people who can read the language. This includes operator precedence.

Re:Interview Gone Wrong by shkkmo on Monday November 04, 2024 @03:54AM (#42039743)
The more complex an expression grows, the less well it works to rely on operator precedence to indicate structure to the reader.

Additionally, I think assuming the reader is aware of every. nuance of the language is an extremely bad idea unless you are and will always be the only developer. If you are doing anything weird or tricky or rare, I'd highly recommend linking the documentation or providing an explanation.

Re:Interview Gone Wrong by nmeofthestate on Monday November 04, 2024 @05:25AM (#42040217)
>I write code for people who can read the language

Programming languages often have syntax that's acknowledged as a source of confusion and bugs and better avoided. The line is not hard and fast, but I write C++ and there's "C++", and then there's "the C++ subset that you should use" (I probably couldn't write C++ code without tools slapping me for using the wrong parts). Operator precedence is debatable but our tools force the use of parentheses to disambiguate.

Re:Interview Gone Wrong by stavros on Monday November 04, 2024 @06:22AM (#42040538)
This is true, and, in this case, "confusion avoided" and "what Python does" coincide. The "double equals doing the right thing" is only confusing if you're both unfamiliar with Python and familiar with language design, which is a very small set of people.

People unfamiliar with programming will assume that "if a == b == c" is only true if all three things are the same, and people familiar with Python will know that that is, indeed, what it means.

Re:Interview Gone Wrong by throwaway313373 on Monday November 04, 2024 @02:02AM (#42039218)
"someone who writes code" is very vague. For someone who writes code primarily in Python this behavior is less surprising than the rest that you described.
Re:Interview Gone Wrong by ball_of_lint on Monday November 04, 2024 @05:12AM (#42040140)
Intentionally so.

I've worked primarily in Python for the last two years and I'd still have to look at a reference to be certain of what exactly this code does. And would probably rewrite it to the expanded, parenthesized form unless the company linter insisted.

Re:Interview Gone Wrong by atoav on Monday November 04, 2024 @03:03AM (#42039515)
I'd argue that many programming languages are not optimized for "people who read code" but they are optimized for programs who read code (compilers, interpreters) and programmers can stockholm-syndrome themselves into believing that it is targeted at them.
Re:Interview Gone Wrong by James_K on Monday November 04, 2024 @04:34AM (#42039931)
Code optimised for an interpreter is often also optimised for the programmer, whose primary job is interpreting code. What makes natural language intuitive is that it doesn't have to be precise. You can assume language does what it is intended to do. A programmer, by contrast, must know exactly what the program does. You cannot assume it does what you want it to do because the computer doesn't know what you want it to do. A language cannot be made less precise by the addition of new rules; it is only made more complicated as there is more to remember. In this example, the person writing the ternary expression had to learn that it exists. If it didn't exist, there would have been less learning to do. Perl is the limit of this process – a language with many rules to appear natural and human-like that it's almost impossible to actually read.

Each rule of a language is a cognitive burden. It can only justify itself if the thing it replaces is a greater burden.

Re:Interview Gone Wrong by dagw on Monday November 04, 2024 @07:27AM (#42040925)
Most programming languages ought not to be optimized for a non-programmer to read

Python 'won' mostly because non-programmers could look at it, more or less understand what was going on, and feel that this was something they probably could learn.

Re:Interview Gone Wrong by alkonaut on Monday November 04, 2024 @02:49AM (#42039440)
What does A == B == C even mean? I mean I know what mathematically A=B=C means. That A, B and C are all equivalent.

But then is the mathematical '=' really a binary operator that maps two numerical inputs to a boolean output? It feels then as an abuse of notation if (A=B)=C doesn't allow A=B to change type?

Because I don't really have much use for a symbol that is some times doing a binary mapping from numbers to booleans and some times becomes some sort of ternary operator which maps 3 inputs to one boolean.

Re:Interview Gone Wrong by joveian on Monday November 04, 2024 @05:34PM (#42046719)
Maybe consider chained comparison operators early textual replacement along the lines of the C preprocessor, although the exact textual replacement would involve temporary variables to avoid multiple side effects and be more complicated than just "(a) == (b) and (b) == (c)". (a==b)==c does not expand, only the version without parentheses expands, so you can still do the boolean comparison if you want.
Re:Interview Gone Wrong by TrainedMonkey on Monday November 04, 2024 @04:13AM (#42039842)
I have some experience in computer language design. The issue here is that `a==b==c` expression is magical - that is it does not follow from extending comparison binary operator. Specifically, `==` is a binary operator that that compares an expression before and after and returns a boolean. In this case, A==B==C is a trinary comparison operator. This is normally ok, except it's rare and the symbol it is using is overloaded with binary comparison operator, so the people will be confused.

This actually gets weirder - in python you can make an arbitrary long comparison operator - it's called comparison chaining - https://www.programiz.com/online-compiler/6uyqb52IVH8if . It works with a lot of operators - https://www.geeksforgeeks.org/chaining-comparison-operators-...

Once you know how it works and are used to it, I think it makes the code easier to parse... but there are heavy downsides. For example, it's not clear how short circuiting works. I've used python a bunch and logically I expect ```side_effect3()``` to not be evaluated if results of 1 and 2 are not equal: ```side_effect1() == side_effect2() == side_effect3()```. However, I do not know that for sure, while in other languages I would be able to reason about this from basic principles.

Re:Interview Gone Wrong by Ukv on Monday November 04, 2024 @04:53AM (#42040028)
> the symbol it is using is overloaded with binary comparison operator, so the people will be confused

I think most people would expect expressions like `5 < x < 10` to work as they do in math, without necessarily thinking about it in terms of being an overloaded binary operator. The result in other languages that `5 < 12 < 10` = `true < 10` = `true` is more surprising, just that we've (perhaps by being bitten by it early on) gotten used to it.

Re:Interview Gone Wrong by redditor98654 on Monday November 04, 2024 @05:59AM (#42040433)
Yeah, but should that math equivalence hold for programming though? Programming is different. x=x+1 is perfectly legal in programming but does not make sense in algebra math and could confuse mathematicians.
Re:Interview Gone Wrong by lordnacho on Monday November 04, 2024 @06:25AM (#42040550)
Maybe = should actually be := which it is in some languages?

x = x + 1 sounds wrong if you are in math mode

x := x + 1 is uncommon in math and you can take it to mean "set x to whatever is currently x, plus one"

But it's also true that you need to be able to accept certain conventions when changing fields.

Re:Interview Gone Wrong by Ukv on Monday November 04, 2024 @07:09AM (#42040806)
> should that math equivalence hold for programming though?

It's not a hard rule that overrides all other considerations, but I do think Python's choice to follow math is the right decision here.

I'd claim if you teach someone `<` then show them `5 < x < 10` for the first time, they're far more likely to go with the math interpretation than the C one. That is, beyond the fact that someone's familiarity with math reinforces this intepretation, the reason for math using this interpretation in the first place is because it aligns with intuition.

It's also just pretty useful. It's common to want to check if a number is between some bounds, like `0 <= i < len`, or that multiple things are equal. In cases where you really do want the C behavior, I'd say you should already write that as `(x == y) == z` for clarity anyway, regardless of whether the language lacks chaining and thus lets you omit the parentheses.

Re:Interview Gone Wrong by sampo on Monday November 04, 2024 @05:28AM (#42040236)
Re:Interview Gone Wrong by andreasmetsala on Monday November 04, 2024 @07:12AM (#42040828)
Is “let me check the docs” an appropriate thing to say in an interview setting?
Re:Interview Gone Wrong by natebc on Monday November 04, 2024 @07:35AM (#42040971)
yes.
Re:Interview Gone Wrong by acdha on Monday November 04, 2024 @08:59AM (#42041566)
It’s what you’d do on the job, right? A place which dings you for checking your work is broken.

I’d expect a raised eyebrow if it was some very basic question about something you’ve claimed to be an expert in but given how many, many bugs over the years stem back to confusion about order of evaluation I would consider it a minor plus if someone said “I think I know but I want be certain”, and a major one if they paired that with mention of defensive coding practices like adding tests covering cases where it’d matter and structuring the code so there are no side effects hidden in the test (e.g. the code as written is fine but if it was f1() == f2() == f3() I’d probably write it as a clearer multi-line if block when there are side effects to those calls to make obvious when I’m intentionally not doing them in every case).

Re:Interview Gone Wrong by bityard on Monday November 04, 2024 @09:10AM (#42041660)
It absolutely is.
Re:Interview Gone Wrong by ljm on Monday November 04, 2024 @09:34AM (#42041851)
I'd prefer a candidate to check the docs or look something up if they were unsure, because that would also provide useful information to me as an interviewer.

Although I'd also want that benefit of the doubt for myself. I'm quite familiar with how Ruby handles collections, for example, but if I tried to use `slices.Collect` in Go the same way as `Enumerable#collect` in Ruby I'd end up stuck and would need clarification.

Re:Interview Gone Wrong by dsr_ on Monday November 04, 2024 @10:39AM (#42042532)
Yes.

Extra points if you are familiar enough with the documentation to find the answer quickly.

Re:Interview Gone Wrong by renegade-otter on Monday November 04, 2024 @09:22AM (#42041749)
It's a completely useless question - it's language trivia. It's like asking "what happens if you do X in database Y version Z and up". Who cares? It's something you figure out when you get there. There are tons of examples of something like this where it's language-dependent, so don't even bother memorizing this stuff.

I once passed an interview where it was all just Spring documentation questions. I had never built a Spring app in my life, I just "read the docs" five times.

Re:Interview Gone Wrong by phkahler on Monday November 04, 2024 @09:57AM (#42042079)
>> but there are heavy downsides. For example, it's not clear how short circuiting works.

Opinion: Code that depends on side effects like that in a conditional is BAD. It is not an optimization, it's an obfuscation.

Re:Interview Gone Wrong by ashu1461 on Tuesday November 05, 2024 @10:18AM (#42052138)
Agree, also by reasoning from basic principles adding extra brackets ( ) should not change the output result - at least this is what our brain is programmed to believe, but in the case of chained operations it will.

Example

(a==b)==c would be different from a==b==c

Re:Interview Gone Wrong by tmtvl on Monday November 04, 2024 @04:47AM (#42040004)
Yet another way in which Lisp is vastly superior to any and all blub languages:

  (= 2
     (+ 1 1)
     (sqrt 4))
  ; => t

  (< most-negative-fixnum 0 most-positive-fixnum)
  ; => t
Re:Interview Gone Wrong by Aachen on Monday November 04, 2024 @06:18AM (#42040520)
I have no idea what this does or if you are joking. Could use some explanation
Re:Interview Gone Wrong by gus_massa on Monday November 04, 2024 @07:07AM (#42040785)
In Lisp, you put the "operator" first, so instead of

  2 + 3 + 4
you write

  (+ 2 3 4)

This is nice for associative operations like + or *, very very very slightly confusing for - and /

---

You use the same style for comparisons, so instead of

  2 == 3 == 4
you write

  (== 2 3 4)

To support the fist "infix" syntax, you need some magic in the == operator. But the second "prefix" syntax is totally natural and you need no magic in the parser or the operator.
Re:Interview Gone Wrong by tmtvl on Monday November 04, 2024 @08:17AM (#42041251)
> very very very slightly confusing for - and /

It's fine if you DNF it:

  (- a b c) => (+ a (- b) (- c))
  (/ a b c) => (* a (/ 1 b) (/ 1 c))
Re:Interview Gone Wrong by Aachen on Monday November 04, 2024 @11:12AM (#42042918)
I knew that Lisp does this operator first thing, it's everything around this that I'm not familiar with. What does ; do? Is the => an arrow or greater than or equal to? What is t? Do I guess correctly that most-negative-fixnum is like INT_MIN?
Re:Interview Gone Wrong by tmtvl on Monday November 04, 2024 @11:51AM (#42043407)
The semicolon is Lisp syntax for comments. T is the way you write true in Lisp. Most-negative-fixnum is the most negative number Lisp can represent without promotion to bignum (so it can be int_min if int is roughly equivalent to size_t).
Re:Interview Gone Wrong by ahoka on Monday November 04, 2024 @07:49AM (#42041049)
In the Lisp family of languages these comparisons are not implemented as operators, but functions.

The functions behave like:

= all the same

/= all different

< monotonically increasing

> monotonically decreasing

<= monotonically nondecreasing

>= monotonically nonincreasing

Also * is product and + is sum.

Re:Interview Gone Wrong by ahoka on Monday November 04, 2024 @07:57AM (#42041109)
So Python is a Lisp?
Re:Interview Gone Wrong by quietbritishjim on Monday November 04, 2024 @08:01AM (#42041133)
> If you know nothing about Python or coding, and you see a=b=c, you'd think that's true when all three a,b,c are the same.

Sure, that's true if you literally know nothing about coding. But that is not a very common audience for reading code. You only need to spend about 10 minutes coding to realise that the compiler follows fixed rules rather than making an LLM-like guess as to the meaning. If you get that far then most people (admittedly after a bit more 10 minutes) go on to realise that the only way to know what code does is carefully pick it apart step by step, rather than glance at it and guess yourself.

I love Python dearly, but this rule was a misstep on my opinion.

Re:Interview Gone Wrong by ninetyninenine on Monday November 04, 2024 @09:25AM (#42041782)
The problem isn’t meaning or intent its inconsistency of operator behavior.

1 + 1 + 1 has different operator behavior then 1 == 1 == 1. The operations here are not consistent and it’s not clear what happens if I overload the operators.

On the surface level python looks more beautiful. But mathematically speaking python is actually more ugly and more inconsistent with weird rules to make things look a certain way.

Re:Interview Gone Wrong by eacapeisfutuile on Monday November 04, 2024 @07:39PM (#42047597)
If you know nothing about Python or coding it is not really relevant as the code is probably read and written more by those who know coding and/or Python?