7 ms·
>Not indentation based (counter-inspired by Python), since it is brittle. But also not require semicolons. Inspired by Ruby, and semi-colon-free JS. I actually
by 999900000999 5y ago
>Not indentation based (counter-inspired by Python), since it is brittle. But also not require semicolons. Inspired by Ruby, and semi-colon-free JS.
I actually love indentation, Python forces you to write readable code.
Python that complies and is faster than C would be my dream language. I'd also like it to work directly with Unity and Flutter, replacing C# and Dart.
- Avshalom 5y agoPython forces you to write indented code, that's basically nothing to do with readability.
- 999900000999 5y agoHypothetically your on a team with a bunch of junior devs. Your doing a PR review, assuming this dev is sloppy, would you rather it be in C++ or Python ? I didn't like Python or Ruby at first, but now whenever I need to write a small tool, it's Python. Before Python I was using JavaScript, but I've fallen in love with how clean Python is
- dragonwriter 5y ago> Your doing a PR review, assuming this dev is sloppy, would you rather it be in C++ or Python ? I'd rather it be in Python because I haven't touched C++ with any seriousness since the 1990s. But not because of formatting (in either case, I’d prefer a standardized code formatter be in use, which gets you a lot farther than Python’s whitespace sensitivities alone.)
- 999900000999 5y agoWhen did you start as a developer, are you still an individual contributor or did you have to move into management? I really do want to program, until I retire because I really hate trying to manage people.
- dnautics 5y agoAm I wrong in remembering that python indentation a choice made loosely, "to force noob programmers to write well-organized, legible code"?
- dkarl 5y agoI think GvR observed that beginning programmers often got confused when the indentation of their code didn't correspond to the syntactic nesting implied by brackets, etc. They found the indentation to be more salient and easier to interpret, so he designed the language to cater to that.
- dnautics 5y agoThanks for confirming. Sorry for the crudeness.
- int_19h 5y agoThe use of indentation for program structure predates Python; it comes from ABC: https://en.wikipedia.org/wiki/ABC_(programming_language) https://en.wikipedia.org/wiki/ABC_(programming_language)
- dnautics 5y agoNobody said python was first. In fact "making the choice" weakly (but not absolutely) implies that someone else did it before
- int_19h 5y agoSorry, I wasn't clear enough: ABC is not just some random language - it's one that GvR worked on before Python, and he specifically said in several interviews that it's where Python originated from. As you correctly surmised, ABC was a teaching language, hence the choice to go with indentation.
- dnautics 5y ago
- seanmcdirmid 5y agoIn visual studio (not code), there is an extension to render braces as very small, almost invisible. So with that extension and standard IDE enforced indentation (depending on those braces you can barely see), C# code almost looks like python code.
- forrestthewoods 5y agoIndentation makes copy/paste bugs too hard. It’s difficult to copy/paste when the source and destination are at different indentation levels. I strongly prefer braces. But I also increasingly languages with a standard style and formatter. Go and Rust both do this well.
- kjeetgill 5y ago> Indentation makes copy/paste bugs too hard. I love Python's indentation based blocks (in Python) but this is the biggest issue I've had with it. That and maybe occasionally when word wrapping. I think Python's general terseness mitigates most of the reasons I might miss blocks in C or Java.
- boardwaalk 5y agoSelecting and hitting tab or shift-tab (or <, > in Vim) to adjust the indent level isn't all that difficult.
- kinjba11 5y agoTools like Prettier can automate this with curly-brace languages, so indentation (tabs or spaces?) stops being something you waste brain power on, similar to how code editors will match the current line's indent on the next line when you press enter even though it's just "press tab a few times". It adds up. // just copied some code into this while block, oops I copied an extra space out front... while (...) { if (...) { ...} } // after Ctrl+S tied to autoformat while (...) { if (...) { ... } }
- xigoi 5y agofor line in lines_to_paste: paste(current_indent .. line)
- dragonwriter 5y ago> Indentation makes copy/paste bugs too hard. Making copy/paste bugs too hard discourages copy/pasting code rather than reading and rewriting, so, mixed bag there, IMO.
- geewee 5y agoI don't mind indented code when writing or reading code. However I think indentation often becomes a pain when copying code from e.g. StackOverflow - the indentation is never quite right.
- pjerem 5y ago> copying code from e.g. StackOverflow Are people really doing this copy from SO thing ? I always thought it was just a meme.
- hinkley 5y agoIf Python had left the tab character out of the grammar, we wouldn’t still be having this argument checks notes thirty years later. Vi and emacs had ways to do soft tabs. It’s not like it was an editor problem.
- wallscratch 5y agoI like how python’s indentation scheme makes code look nearer, but I find it scary in the following case: for i in range(0, m): for j in range(0, n): doInner() doOuter() A single tab here in the 4th line produces syntactically correct but logically incorrect code. I find this scary. I guess it would be easy to just write your own preprocessor that requires curly braces everywhere and removes them for the python interpreter, but eh.
- qsort 5y agoYeah, I don't like Python's approach in re syntax either. In addition to your example, it backfires for things like multiline formulas (need to add parentheses or escapes) or lambdas. Braces (or even 'end' like ruby) would be much more practical.
- dan-robertson 5y agoAlso you need white space diffs to be turned on to spot this change in code review, and this is particularly bad if indentation changed for another reason.
- lolinder 5y agoTo be fair, you probably always want whitespace diffs turned on if reviewing Python, since indentation is meaningful.
- Jcowell 5y agoWon’t there be an indentation error on line 3?
- xigoi 5y agoIs this really different from braces? If you have for i in range(0, n) { for i in range(0, n) { doInner() } doOuter() } and you accidentally move the first closing brace below the next line, precisely the same will happen.
- 5y ago
- ummonk 5y agoAutoformatters are a thing
- Akronymus 5y agoI like indentation in certain languages. Like f#. Because for me, the combination of dynamic typing, statement based and significant whitespace is just bad.
- k__ 5y agoAs far as I know, ML-like languages use a saner approach to semicolon/brace-less syntax than Python.
- ianbicking 5y agoLater on the author wants an autoformatter as part of the core language tooling, ala gofmt. I think with autoformatting the code ends up as readable as Python... the code can still be unreadable, but not because of misleading indentation! (Well, I still am offended by the vertical inefficiency of K&R style bracing, but presumably the language wouldn't copy that style...)
- allknowingfrog 5y ago"Python that compiles" is not too far off from describing Nim. https://nim-lang.org/ https://nim-lang.org/
- 999900000999 5y agoCrystal also looks nice. Which one is more advanced
- girvo 5y agoThey're similar in terms of how advanced they are. I find Nim preferable, personally, and has a pretty excellent batteries-included standard library, as well as robust tooling and third party library support. That said, I'm biased, and haven't used Crystal for a couple years. Anecdotally, I see more uptake of Nim than I do of Crystal, but again, I'm biased and it's likely because I'm looking for it, rather than it being a fact of the world.
- 999900000999 5y agoA very lazy way to do this would be to compare industry adoption rates. In three of five FAANG companies are using Nim, then it's probably a stable technology.
- awild 5y agoThe problem is that when auto formatters completely wreck with the semantics of your code because of a missing or misplaced character. It has actually happened often and drives me nuts every time. I prefer to write code the way I want/need then apply a formatter to unify my code with that of myself and colleagues.
- xigoi 5y agoIf a formatter changes the semantics of code, that's a bug in the formatter.
- MauranKilom 5y agoDepends on what the formatter does when the code is syntactically invalid in the first place. It could just error out, but that might be annoying if the error is in an unrelated part of the file. So a formatter might opt to format on a best-effort basis even in the presence of syntax problems, but then there's the risk of creating different semantics than intended (once the syntax problem is fixed).
- xigoi 5y agoEvery formatter I know errors out if the file is syntactically invalid.
- awild 5y agoThis isn't about syntactical errors, per se. One example I can remember was an empty if or for loop body where everything beyond it was raised into its scope. I'm taking about pycharm formatter specifically here. This does also happen with braces sometimes, but this would've simply been a syntax error in most other languages or just pulled up the single next line not the entire following code.
- xigoi 5y agoIf you write something like this: if foo: bar That's a syntax error, and a good formatter won't do anything about it. If PyCharm's formatter changes it, I wouldn't use it.
- dragonwriter 5y ago> Python forces you to write readable code. No, it doesn't. It's quite possible to write hard-to-read Python.
- 999900000999 5y agoCan you seriously say it's easier to write hard to read python than writing hard to read Rust or C++ ? Python is a remarkable language because it's just so clean. I don't get this indent issue all of you keep complaining about, just use a good IDE.
- brandmeyer 5y agoDecorator over-use can lead to some difficult-to-read Python. Doing just about anything imperative at import-time makes it hard to reason about what happens when. Even experienced Python programmers can get tripped up by the import system's order of evaluation.
- Schroedingersat 5y ago> Can you seriously say it's easier to write hard to read python than writing hard to read Rust or C++ ? Dynamic typing and passing around *kwargs make this so trivial often the documentation doesn't even help and your IDE can't help you without executing the entire program.
- int_19h 5y agoThere are certainly some things about Python that make it easier to write hard-to-read code in it compared to Rust. One example is that booleans in Python are considered integers (0 and 1) for all purposes, including arithmetic: True+True produces 2 with no warning whatsoever. Even in C++, where this is also legal, you usually also get a warning. And conversely, Python is much more flexible when it comes to interpreting other things as booleans: on top of implicitly treating null pointers and integers as false like C does, Python does the same to empty strings and even collections. Then there's the part where "and" and "or" don't just return true/false, but rather the value of one of the operands, which needs not be a boolean. Sequence comprehensions are also a mighty tool for producing unreadable code if desired, especially if you nest them and/or use the conditional operator. This is more so in Python due to the unusually ordered syntax for both. Here's a fizzbuzz code golf example that combines these techniques: [f"{(not x % 3) * 'Fizz'}{(not x % 5) * 'Buzz'}" or x for x in range(1, 20)]
- lifthrasiir 5y ago> Python forces you to write readable code. Even in the REPL, which is extremely annoying. To this date Python REPL examples tend to have this kind of artifical comments (quoting from [1]): >>> class Weekday(Enum): ... MONDAY = 1 ... TUESDAY = 2 ... WEDNESDAY = 3 ... THURSDAY = 4 ... FRIDAY = 5 ... SATURDAY = 6 ... SUNDAY = 7 ... # ... @classmethod ... def from_date(cls, date): ... return cls(date.isoweekday()) Enforcing indentation is generally a good thing, but it should be possible to switch it off or loosen it up when it ceases to be good. Which is impossible with a Python-like indentation-only syntax. [1] https://docs.python.org/3.11/howto/enum.html#basic-enum-tutorial https://docs.python.org/3.11/howto/enum.html#basic-enum-tuto...
- bmitc 5y agoYou should take a look at F#. While not faster than C necessarily, it beats Python in every aspect aside from the availability of certain libraries and can look quite similar to Python.
- Too 5y agoWhile i normally prefer indentation to demark function scope, it has one huge drawback: When writing callbacks, especially chained promises, Python has no way to express the following in a convenient way. foo().then(f => { return bar(f) }).then(b => { print(b) }) Lambdas only allow a single expression. Defining the callback-function upfront leads to lots of boilerplate and reading the code out of order. Async reduces the need of callbacks but instead leads to colored function problem.
- garethrowlands 5y agoAnonymous functions aren't well supported in python, for sure.
- dragonwriter 5y ago> While i normally prefer indentation to demark function scope, it has one huge drawback: When writing callbacks, especially chained promises, Python has no way to express the following in a convenient way. foo().then(f => { return bar(f) }).then(b => { print(b) }) Huh? Assuming an identical API structure, it's syntax for exactly that would be: foo().then(bar).then(print) > Lambdas only allow a single expression. Sure, but your problem code uses only single-expression lambdas with superfluous blocks (where, in fact, the lambdas are also superfluous), so its literally the worst possible thing to say that Python can't express as cleanly. Also, single-expression lambdas can handle a lot, because expressions are easy to combine to arbitrary levels, it's only multistatement imperative blocks you can't do in a lambda, but Python has expression forms that cover lots of uses of imperative blocks already.
- Too 5y agoSorry i only used a single expression in the example for brevity. Normally they contain a lot more than one. I thought that would be obvious from the context, the form you presented would fall into the “define function upfront” category.