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!
Answer from Brandon Rhodes on Stack Overflow
I’m currently teaching Python fundamentals and ran into a confusing explanation about print().
My understanding has always been that print() is a built-in function in Python. It’s part of Python’s built-ins and you can call it directly like:
print("Hello")
But my education coordinator explained it differently. He said that print is a method because it’s already there, and that functions are things you create yourself. He also said that methods take arguments and functions take parameters.
That explanation confused me because everything I’ve read says:
-
print()is a built-in function -
Methods are functions attached to objects or classes (like
"hello".upper())
So now I’m wondering:
-
Is there any context where someone would reasonably call
print()a method in Python? -
Am I misunderstanding the difference between functions, methods, arguments, and parameters?
I’d appreciate clarification from more experienced developers because I want to make sure I’m explaining this correctly to students.
Thanks!
Why is print not a function in python? - Stack Overflow
So how does Print function work in Python?
Can you print something in python without any built in functions or modules? - Stack Overflow
python - print function is from which module - Stack Overflow
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.
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?
Yes. They're built-in functions (or in the case of list, a built-in class). You can explicitly import the __builtin__ module (Py2) or the builtins module (Py3) if you want qualified access to the names, but by default, those modules are searched whenever an attempt to access a global name doesn't find the name in the module globals. They're not normally needed though, per the docs:
This module is not normally accessed explicitly by most applications, but can be useful in modules that provide objects with the same name as a built-in value, but in which the built-in of that name is also needed.
The print function comes from the builtins module.
You can find its documentation here.
Here is an example session.
I first check what module print comes from,which is stored in its __module__ attribute.
Then, I import the builtins module, and checks if its print function is the same as the prefix-less print.
>>> print.__module__
'builtins'
>>> import builtins
>>> builtins.print("hello")
hello
>>> print is builtins.print
True