3 ms·
> looking back, I'm mostly amused that Guido van Rossum (the developer of the Python programming language) wanted web browsers to collapse multiple spaces to a
by thaumasiotes 3y ago
> looking back, I'm mostly amused that Guido van Rossum (the developer of the Python programming language) wanted web browsers to collapse multiple spaces to a single space after periods. Python, notably among programming languages, treats whitespace as significant.
It does so by collapsing any amount of space into single INDENT and DEDENT tokens, so I'm not sure what the irony is supposed to be. If the last line started with 8 spaces, Python will not distinguish between the next line starting with 10 spaces and the next line starting with 50.
- pwdisswordfishc 3y agoHave you ever seen a nested control structure?
- simiones 3y agoIt's a bit funny because it's actually hard to represent Python code in regular HTML, exactly because of this collapsing. For example, here is some Python code typed correctly in the comment box but rendered without the code formatting support (using a | at beginning of line to prevent the code formatting to kick in): |def foo(flag): | if flag: | print("this is indented") | print("is this?") If you check the HTML, it looks like this: <p>|def foo(flag):</p> <p>| if flag:</p> <p>| print("this is indented")</p> <p>| print("is this?")</p>
- distcs 3y ago> it's actually hard to represent Python code in regular HTML It is not hard. Just use the right tag. You want the <pre> tag here. Here, fixed it for you: def foo(flag): if flag: print("this is indented") print("is this?")
- BeetleB 3y ago> If the last line started with 8 spaces, Python will not distinguish between the next line starting with 10 spaces and the next line starting with 50. Perhaps I misunderstood your comment, but this is quite wrong. If I have a block that's indented at 8 spaces, and the next line is 10 spaces, but should be in the same block, it will definitely throw an IndentationError.
- thaumasiotes 3y agoYes, you've misunderstood. If the last line started with 8 spaces, Python will see a difference between (the next line starting with 8 spaces), (the next line starting with 6 spaces), and (the next line starting with 10 spaces). Option 1 does nothing. Option 2 produces a DEDENT token. And option 3 produces an INDENT token. In the same scenario, Python does not distinguish between (the next line starting with 10 spaces), which produces an INDENT token, and (the next line starting with 50 spaces), which produces the same INDENT token. The amount of whitespace isn't relevant. All that matters is the comparison between one line and the previous line.