datacarpentry / python-ecology-lesson

Data Analysis and Visualization in Python for Ecologists
https://datacarpentry.org/python-ecology-lesson
Other
160 stars 307 forks source link

Semicolons are not typically used in Python code and may cause confusion #604

Open maneesha opened 7 months ago

maneesha commented 7 months ago

What is the problem?

Semicolons aren't wrong but are not commonly used in Python code and have not been introduced in this lesson. Line breaks and indents are more critical to properly formatted Python code. Consider either explaining why semicolons are used here and no where else, or remove the semicolons.

Location of problem (optional)

https://datacarpentry.org/python-ecology-lesson/02-starting-with-data.html#quick-easy-plotting-data-using-pandas

quist00 commented 7 months ago

To the best of my knowledge, the purpose in this case is just to suppress arcane output about memory space from the figure that is generated. If removing them means regenerating all those screenshots to match, then I would prefer explaining how they can be used and how they are being used here. Do you know specifically if that suppression of output is an anomaly in in Juptyer that we are taking advantage of? Like would it only output that memory address information for the last item and by using a semi-colon we are tricking it into thinking our figure is not that last item in the cell?

maneesha commented 7 months ago

I don't know why semicolons are used here. I don't have a preference about removing them or adding an explanation about them.

quist00 commented 7 months ago

For later reference. It does seem to be a trick to assign None to the last item in the cell to suppress the printing of it.

Semicolon in the interpreter Having read the answers, I still miss one important aspect of using semicolons, possibly the only one where it really makes a difference...

When you're working in an interpreter REPL (the Python interactive shell, IDLE, and IPython) the value of the last expression is printed to the screen and usually this is the intended behavior.

Using an expression for side effects But in some cases you want to evaluate an expression for its side effects only, e.g., to see the results of your simulation plotted by matplotlib.

In this cases you (probably) don't want to see the screenful of reprs of matplotlib objects that are sometimes returned by a call to a matplotlib function and, in IPython at least, one of the possibilities you have is to append a semicolon to the overly verbose statement, now IPython sees the input line as composed by two expressions, the matplotlib invocation and a null statement, so that the value of the compound expression is None and nothing is printed to the screen by the interpreter (the other possibility being assignment, as in _ = plot(...) but I find that a bit more intrusive).

Personal remark IMHO, the use of the semicolon to suppress not desired output in the interpreter has become more relevant following the introduction of the IPython notebook, that permits to save the input and the output, including graphical output, of an interpreter session for documentation and eventual reuse.

https://stackoverflow.com/questions/8236380/why-is-semicolon-allowed-in-this-python-snippet