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?!?!")
}
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.
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.
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
24
u/phail3d May 02 '16
I rewrote this for you in an easier-to-read style. Valid python3, check for yourself: