In python everything is an object and you can pass it around easily.
So I can do :
>> def b():
....print "b"
>> a = b
>> a()
b
But if I do
a = print
I get SyntaxError
. Why so ?
In Python 2.x, print is a statement not a function. In 2.6+ you can enable it to be a function within a given module using from __future__ import print_function
. In Python 3.x it is a function that can be passed around.
In python2, print
is a statement. If you do from __future__ import print_function
, you can do as you described. In python3, what you tried works without any imports, since print was made a function.
This is covered in PEP3105
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With