• 8

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

Python is a "whitespace delimited" language. However, the use of semicolons are allowed. For example, the following works but is frowned upon:

print("This is valid");

I've been using python for several years now, and the only time I have ever used semicolons is in generating one-time command-line scripts with python:

python -c "import inspect, mymodule; print(inspect.getfile(mymodule))"

or adding code in comments on SO (i.e. "you should try import os; print os.path.join(a,b)")

I also noticed in this answer to a similar question that the semicolon can also be used to make one line if blocks, as in

if x < y < z: print(x); print(y); print(z) 

which is convenient for the two usage examples I gave (command-line scripts and comments).

The above examples are for communicating code in paragraph form or making short snippets, but not something I would expect in a production codebase.

Here is my question: in python, is there ever a reason to use the semicolon in a production code? I imagine that they were added to the language solely for the reasons I have cited, but its always possible that Guido had a grander scheme in mind. No opinions please; I'm looking either for examples from existing code where the semicolon was useful, or some kind of statement from the python docs or from Guido about the use of the semicolon.

      • 1
    • The one liner example throws SyntaxError. ; can only be used to separate simple statements like x=1;y=2. Trying to separate a simple statement and compound statement like x=1; for i in range(1): print(1) will fail.
    • @bain It does not give a SyntaxError, unless you are on Python3 and forgot to add the parenthesis on the print functions. I updated syntax in the question to Python3 to avoid confusions like this down the road.
      • 2
    • @bain The statement "The one liner example throws SyntaxError" implies that either the example I posted gives a SyntaxError, or all one-liners give a SyntaxError; neither are true. If you try the form I posted it works. Perhaps a better way to phrase your observation is "it appears there are cases where Python does not let you use a for loop with ;, here is an example I found."

PEP 8 is the official style guide and says:

Compound statements (multiple statements on the same line) are generally discouraged.

(See also the examples just after this in the PEP.)

While I don't agree with everything PEP 8 says, if you're looking for an authoritative source, that's it. You should use multi-statement lines only as a last resort. (python -c is a good example of such a last resort, because you have no way to use actual linebreaks in that case.)

  • 21
Reply Report

I use semicolons in code all of the time. Our code folds across lines quite frequently, and a semicolon is a definitive assertion that a statement is ending.

output, errors, status = generate_output_with_errors_and_status(
        , argument_two_to_argument)
    , second_argument);

See? They're quite helpful to readability.

  • 2
Reply Report
      • 2
    • Sorry I would have to disagree. The way you use semicolon is merely your personal preference. In fact, the next line following your code would have no indentation, which serves better (and more pythonic) as an assertion that "the previous line ends; a new line begins". Besides, your using leading comma is also very interesting. Read PEP8 and you will surely find some (more popular) styles for that.
      • 1
    • (1) I have to admit that the "interesting" in my first comment actually meaned "I never saw leading-comma in Python; I don't get it". Sorry I should have been more specific. (2) And then you shared that promoting the leading-comma post, which is really eye-opening, and reasonable - when using Javascript. (3) But I still think all those reasons do not apply to Python: Missing one trailing comma will give you a noisy syntax error (which is good) rather than a sneaky leaking global variable; and Python allows extra comma at last item already.
      • 1
    • Keep in mind that Haskell is Haskell, JavaScript is JavaScript, and neither are Python. What may be a good style for one language may not be good for another. For example, Perl style emphasizes terse style and multiple commands on a line, much of which is literally not possible with Python because of the syntax limitations.
      • 1
    • A better trick to to add readability when a function splits over multiple lines is to move the close bracket to its own final line, un-indented to match the original function. If you do this here to both functions, move argument_one_to_argument to its own line (to match the other argument) and fix the commas (including adding trailing commas) you end up with significantly more readable code than this. pastebin.com/XSS93LpT

Trending Tags