So I am a beginner who was learning python and the first thing I learnt was printing strings. Then later on I learnt about functions and it's only now I realised that print itself is a function. Like when we say print ('hello world') we are basically calling an already existing function and not doing anything new. I didn't realise it till now. But the question I got is what does this original print function contain? How does it work? Is it possible for me to built it from scratch by myself?
Why is print not a function in python? - Stack Overflow
Asking for help understanding the difference between return and print in function
In python, how do I get a def function to print?
Why did python 3 change the 'print' syntax?
Python 2 accepted both :
It didn't really. Try printing two things and you'll see that the parentheses are forming a tuple, and not being interpreted as a function call.
>>> print('foo', 'bar')
('foo', 'bar')
That's very rarely what you want. It just happens to work by luck if you only have a single item because in that case the parentheses are just redundant in the same way as the ones in (4) + 2.
print was a statement in 2.x, but it's a function in 3.x. There are a number of very good reasons for this.
-
You can't use keyword arguments if it's a statement. In 3.x you can easily customize behavior with things like:
print(a, b, c, sep=',', end='|') print('foo', flush=True)Under 2.x you have very little flexibility, and the language had to implement a really inconsistent set of syntax rules to account for customization. For example to print to a file handle other than stdout you have to use this bizarre syntax:
print >>sys.stderr, 'foo'
You never see anything else like that anywhere in Python. Or if you wanted to skip printing the line terminator, you had to put a trailing comma
print a, b, c,
Again, that just looks wrong. When
printis a function, all this garbage goes away and you use standard function calling syntax that works like the rest of the language:print('foo', file=sys.stderr) print(a, b, c, end='') -
You can't use argument splatting with a statement. In 3.x if you have a list of items that you want to print with a separator, you can do this:
>>> items = ['foo', 'bar', 'baz'] >>> print(*items, sep='+') foo+bar+baz
Again, this is just like the previous point — there's no reason that
printshould be special. These are capabilities that other functions have already had forever, so why shouldn't they apply toprintas well. -
You can't override a statement. If you want to change the behavior of
print, you can do that when it's a function but not when it's a statement. For example, if you want to automatically flush the stream after every output, you can do something like:print=functools.partial(print, flush=True)
Now every call to
print(...)acts like you wroteprint(..., flush=True). Or maybe you want to turn every call toprint()into a call to a logging function. The possibilities are endless, but you can't do any of this ifprintis a statement, because then it has its own special rules that are different than everything else in the language, and that's just absurd.
Videos
This is not the answer you are looking for…
But in interest of completeness, suppose you did want to print the code of the function itself. This will only work if the code was executed from a file (not a REPL).
import inspect
code, line_no = inspect.getsourcelines(two)
print(''.join(code))
That said, there aren't many good reasons for doing this.
Your functions already print text, you don't need to print the functions. Just call them (don't forget parenthesis).
def one():
print ("lol")
print ("dood")
def two():
one()
one()
two()
Because Guido has decided that he made a mistake. :)
It has since been corrected: try Python 3, which dedicates a section of its release notes to describing the change to a function.
For the whole background, see PEP 3105 and the several links provided in its References section!
print was a statement in Python because it was a statement in ABC, the main inspiration for Python (although it was called WRITE there). That in turn probably had a statement instead of a function as it was a teaching language and as such inspired by basic. Python on the other hand, turned out to be more than a teaching language (although it's good for that too).
However, nowadays print is a function. Yes, in Python 2 as well, you can do
from __future__ import print_function
and you are all set. Works since Python 2.6.