3 ms·
Well, have you ever run into the x = some_long_expression + another_long_expression problem? In Python you need to be careful to wrap that in
by Tyr42 5y ago
Well, have you ever run into the
x = some_long_expression
+ another_long_expression
problem? In Python you need to be careful to wrap that in () or use a \ before the newline to avoid it parsing as two lines, e.g.
x = some_long_expression;
+ another_long_expression;
In particular, if you have a builder expressions
let x = someBuilder()
.setVal(x)
.build()
You'd need to wrap the entire thing in () to avoid it getting mad, right?
So if you're going to terminate expressions with ), and start them with (, why not just terminate them with ; instead?
Javascript is a "Semicolons Optional" language, but it's actually the worst of both worlds, as you have to be very careful not to wrap lines like
function() {
some stuff;
return
really_long_named_thing;
}
As it'll just insert a ; on the return, returning undefined for you.
- zuj 5y agoLove the examples, thanks for ELI5.
- PhineasRex 5y agoTo be fair, that's really more of a python-specific deficiency than a general problem with significant whitespace. Haskell for example interprets further-indented lines as a continuation of the current expression, only ending the expression when it sees a new line of the same or lesser indentation.
- ReleaseCandidat 5y agoAs I wrote, you have to do similar things when using Haskell (or F# or OCaml) in the REPL. Python just doesn't have a special 'non-REPL' syntax.
- ReleaseCandidat 5y ago> In Python you need to be careful to wrap that in () or use a \ I guess that's because in the REPL Python need's to know whether the line has finished yet or not. OCaml and F# uses two semicolons ';;' for that purpose, but only in the REPL. In Haskell you can use braces and semicolons or '{:' and ':}' in the REPL.
- Tyr42 5y agoIt's not just in the repl.
- emmelaich 5y agoAt least Python will give you a diagnostic. Ruby is worse; even with parentheses ... salary = 100500 newsalary = (salary * 0.02 + salary) newsalary will the same as salary. There are no diagnostics. Yes, you can fix it by moving the + up a line but this should be a purely style thing, not semantics.