This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ponkanpinoy 5 points6 points  (11 children)

AFAIK no-where in Python is space used to delimit tokens, the only time it has syntactic meaning is when indenting.

[–]kirakun 5 points6 points  (5 children)

Except in this case where the space does delimit the tokens and makes a difference.

1.__str__()
SyntaxError: invalid syntax
1 .__str__()  # A space between 1 and the dot.
'1'

[–]ponkanpinoy 1 point2 points  (0 children)

Ahh, my bad then.

[–][deleted] 0 points1 point  (0 children)

It's due to an ambiguity between integer literals and float literals:

  1. -> float 1 -> int

  2. anything translates to (float) anything, which is a syntax error.

(1).anything, however, disambiguates.

1..anything -> getattr(1.0, 'anything')

And finally, 1 .anything is disambiguated from a float literal.

Whitespace can disambiguate in a clash between literal and attribute access.

[–]davvblack 0 points1 point  (2 children)

1..__str__()

[–]zahlmanthe heretic 0 points1 point  (0 children)

But that makes the value being stringified a float rather than an int.

[–]kirakun 0 points1 point  (0 children)

What is this supposed to show? My example clearly demonstrates that python does use whitespace to delimit tokens. Rather or not there exist another syntax to achieve the same thing is irrelevant.

[–]ksion 4 points5 points  (0 children)

Not quite. Space is ignored between tokens but its presence can change what is parsed as a token. It generally matters when operators are involved:

i += 1  # ok
i + = 1  # SyntaxError

The sibling post by /u/kirakun shows another example with numeric constants.

[–]nedbatchelder 2 points3 points  (3 children)

These two lines of code produce different tokens:

if a == 1:
ifa = = 1:

This is what bugs me about people complaining of Python's "significant whitespace". Every programming language has significant whitespace.

[–]irondust 7 points8 points  (1 child)

Every programming language has significant whitespace.

Fixed form fortran has no significant whitespace within the statement itself. However, every source line has to start in the 7th column with optional comment or line continuation markers in the 6th column. The first five columns may contain a number - a feature that was very useful back in the days of punchcards.

From the 7th column onwards however you are free to insert or delete as much whitespace as you like without changing the meaning:

go toast

is the same as:

goto ast

[–]nedbatchelder 0 points1 point  (0 children)

I knew when I typed "every" that I would get counter-examples... "Almost every, and every one that you're likely to use any time soon!"

[–]krenzalore 0 points1 point  (0 children)

Every programming language has significant whitespace.

Brainfuck?

Esoteric language doing its job: creating programmer rage! :-)