Hacker News new | past | comments | ask | show | jobs | submit login

Python is somewhat different, in that a newline is simply a statement separator. Semicolons aren't inserted, as far as I know, they're simply an alternate statement separator.

Python does make some exceptions, in that newlines within balanced paratheses and brackets are not treated as statement separators. But outside of that, this is what they mean.

As an example of the difference, consider the following JS:

    if
    (0)
    statement()
This is legal JS. Now consider the roughly equivalent Python:

    if
    0:
    statement()
This is not legal python, because the newline after the "if" terminated the statement, and a bare "if" is not a legal statement.

Another example. This is legal JS:

    obj
    .method()
As it is parsed as a single statement in JS. However, it is not legal Python, as the newline after "obj" definitively ends the statement, and ".method()" by itself is not a legal statement.

In short, Python can always say "this newline is the end of a statement", except for some very easy-to-detect cases. JavaScript, on the other hand, parses until it encounters an error, then goes back to insert the semicolon and tries again. This implies some additional overhead. I don't imagine it's a significant amount at all, but there is at least a good reason to think that this might apply to JS but not a language like Python.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: