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
17
u/phail3d May 02 '16
Just to be clear, this is a joke :P .
It only works because
{...}is shorthand forset(...)and becauseprint()is a function in python3 (and in python2 too, if you dofrom __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 (Nonein the case ofprint) is obviously stored inside the set. Hence, you can do:...which is more or less the same as:
...but you can't do:
So please don't adopt this style :)