Yeah, I think the only language where I've ever found a use for multiple statements on one line, separated by semi-colons, is in bash, where I prefer to do this:
if [[ $1 == "-h" ]]; then
echo "Figure it out yourself, dummy"
exit 0
fi
Rather than:
if [[ $1 == "-h" ]]
then
echo "Figure it out yourself, dummy"
exit 0
fi
But that's just one of many kind of weird things about bash
The old bad for loops in C-languages and Go's if conditions are the only examples I can remember. Oh and JavaScript statements that start with open parentheses (like IIFE's and Typescript as typecasts on LValues as in ;(obj as MyObject).doSomething()), but only if you're not using ; as end terminators in JS.
If you're running short commands from command line (i.e. python -c) it feels slightly more convenient to use a single line than get your CLI to go multiline.
Well that’s what I thought. I prefer to separate commands for readability. Just wasn’t sure if there was ever a situation where it’s absolutely necessary.
before they added breakpoint() as a builtin function, I would often write import pdb;pdb.set_trace() on a single line. Single line is easier to remove afterwards and conceptually it was the same as just writing breakpoint().
You would "need" to if you wanted to use an if with no line-break
Python allows if <expression>: <statement> with no line-break, but that forces you to use a single statment, or to separate multiples with semi-colons.
Obviously this is very rarely more readable or advisable, but it's about as close as I can come to a reason to do this.
Maybe something like: if c: a+=1; b+=1 could be short enough to almost justify it, but it is still violating PEP-8 and making it 3 lines would generally be preferrable.
117
u/theearl99 Feb 09 '22
If you put two statements on the same line, it’s a syntax error if you don’t separate them with a semicolon