Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is print a function in Python?

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 ?

like image 218
tarashish Avatar asked Aug 03 '12 16:08

tarashish


2 Answers

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.

like image 152
Jon Clements Avatar answered Oct 08 '22 01:10

Jon Clements


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

like image 39
Daenyth Avatar answered Oct 08 '22 01:10

Daenyth