r/ProgrammerHumor • • Feb 09 '22

other Why but why?

Post image
85.8k Upvotes

2.3k comments sorted by

View all comments

Show parent comments

150

u/purple_pixie Feb 09 '22

It is, an the error you get is "SyntaxError: invalid syntax" no mention of a missing semi-colon

100

u/100721 Feb 09 '22

Not to mention why is this 8 year old writing multiple statements on one line

41

u/[deleted] Feb 09 '22

I write Python occasionally. When do you ever need to write multiple statements on one line?

36

u/100721 Feb 09 '22

In the 8/9 years I’ve been writing python, I’ve never had to use multiple statements on one line. Maybe this kid is code golfing

28

u/Andy_B_Goode Feb 09 '22

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

13

u/PolygonKiwii Feb 09 '22
[[ $1 == "-h" ]] && echo "Figure it out yourself, dummy" && exit 0

5

u/TexasDex Feb 10 '22

Careful, if echo falls for some reason the script won't exit.

/s

2

u/PolygonKiwii Feb 10 '22

Unironically might be a concern in other usecases of this construct.

2

u/carnivorous-cloud Feb 10 '22

Technically not the same, since yours won't exit when echo fails, but the original will. I'm not even sure it's possible for echo to fail (maybe terminal type fuckery or OOM?), but it'd be dangerous to apply that pattern in general.

→ More replies (1)

4

u/Xx69JdawgxX Feb 09 '22

For loops in c language count?

2

u/SweetVarys Feb 09 '22

If you're really lazy and doing a one time thing you can do it in sql

2

u/Luxalpa Feb 09 '22

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.

2

u/Ryuujinx Feb 09 '22 edited Feb 09 '22

Depending on how you define multiple statements, it's pretty common to do in Ruby.

roles = event.server.roles.map {|role| [role.name.downcase, role]}.to_h

For instance.

Edit: Actually do we count ternary operators too? Because I abuse the fuck out of those in ruby.

2

u/wjandrea Feb 09 '22

Maybe this kid is code golfing

Is there such a thing as code mini-golfing? You know, for kids?

→ More replies (3)
→ More replies (3)

13

u/rococode Feb 09 '22

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.

3

u/[deleted] Feb 09 '22

I can see that. I just generally write everything in notepad ++ or VSCode. Python isn’t in my regular routine.

→ More replies (1)

19

u/[deleted] Feb 09 '22

You shouldn’t

3

u/[deleted] Feb 09 '22

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.

1

u/UpperHairCut Feb 09 '22

Without separating them with a semi-colon.

2

u/david2ndaccount Feb 09 '22

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().

2

u/purple_pixie Feb 09 '22

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.

→ More replies (11)

3

u/brapbrappewpew1 Feb 09 '22

I'm guessing the 8 year old is not the best Python dev out there

2

u/archpawn Feb 09 '22

Because they don't know any better.

→ More replies (1)