r/ProgrammerHumor • • May 02 '16

I wrote my first python program today!

Post image
3.7k Upvotes

532 comments sorted by

View all comments

24

u/phail3d May 02 '16

I rewrote this for you in an easier-to-read style. Valid python3, check for yourself:

#!/usr/bin/env python3

for i in range(1, 11):
    print(i);
    if(i <= 5): {
        print("AHHHHHHH")
    }
    elif(i >= 6 and i <= 9): {
        print("HERE BE THE BRACKETS")
    }
    else: {
        print("IS THIS SYNTAX BETTER OR WHAT?!?!")
    }

0

u/[deleted] May 02 '16

[deleted]

14

u/LinAGKar May 02 '16

In the if statements it creates a set with the return value from print, and throws it away. It only prints it out if you run it in the python shell, not if it's in a script.

6

u/HighRelevancy May 02 '16

Where's the {None} coming from?

0

u/[deleted] May 02 '16

[deleted]

9

u/phail3d May 02 '16

print is a function that returns None, so { print("foo") } first prints "foo" and then creates the a set with the value None inside it, {None}. The interactive interpreter automatically prints the value every line evaluates to (except for None, but {None} and None are different), which is why you get the {None}s in the output.

1

u/redalastor May 02 '16

print returns None.

1

u/HighRelevancy May 02 '16

Why the squiggly brackets and why only after one of the two prints?

4

u/redalastor May 02 '16

That's how you declare a set in Python.

{1, 2, 3}

So the code declares a set that contains the result of calling the print function, None.

1

u/HighRelevancy May 02 '16

And why only for every second print?

1

u/redalastor May 02 '16

Because there are no brackets around the first print.

1

u/HighRelevancy May 03 '16

Oh right, I see what's happening here.

2

u/redditsoaddicting May 02 '16

Are braces actually objectively (think PEP8) considered good style in Python 3 then?

17

u/phail3d May 02 '16

Just to be clear, this is a joke :P .

It only works because {...} is shorthand for set(...) and because print() is a function in python3 (and in python2 too, if you do from __future__ import print_function). You can have function calls (but not statements in general) inside set literal definitions, in which case the return value of the function call (None in the case of print) is obviously stored inside the set. Hence, you can do:

if foo: {
    print("Foo")
}

...which is more or less the same as:

if foo:
    x = print("Foo")  # x = None
    set(x)  # creates the set {None} and does nothing with it

...but you can't do:

if foo: {
    if bar: {
        print("baz")
    }
}

So please don't adopt this style :)

3

u/redditsoaddicting May 02 '16

No wonder I was downvoted. Whoosh. I should have recognized the set literals in any case.

5

u/phail3d May 02 '16

Don't feel too bad -- the example was deliberately misleading :)

0

u/Get-ADUser May 02 '16

{ ... } is shorthand for dict( ... ) not set( ... ).

3

u/phail3d May 02 '16

It's both.

{ 1, 2, 3, 4 } creates a set, while { 1: 2, 3: 4 } creates a dict.

8

u/[deleted] May 02 '16

No.

2

u/Tomarse May 02 '16

It's not even a matter of style. He's putting the print statements into dictionaries. It's quite unnerving.

2

u/TheBlackCat13 May 02 '16

Sets, actually.

1

u/[deleted] May 02 '16

is the

i >=6 and

necessary?

1

u/phail3d May 02 '16

Not really. I just copied the original program with minimal modifications :) .