why print() in python3 instead of just print | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why print() in python3 instead of just print

what is the benefit of print function? why python 3 uses print() function instead of just print statement. can anyone explain me why it was modified in python3?

6th Nov 2016, 5:53 PM
Iqbal Mohammad Rhidwan
Iqbal Mohammad Rhidwan - avatar
2 Answers
+ 4
The print statement has long appeared on lists of dubious language features that are to be removed in Python 3000, such as Guido's "Python Regrets" presentation [1]. As such, the objective of this PEP is not new, though it might become much disputed among Python developers. The following arguments for a print() function are distilled from a python-3000 message by Guido himself [2]: 1- print is the only application-level functionality that has a statement dedicated to it. Within Python's world, syntax is generally used as a last resort, when something can't be done without help from the compiler. Print doesn't qualify for such an exception. 2- At some point in application development one quite often feels the need to replace print output by something more sophisticated, like logging calls or calls into some other I/O library. With a print() function, this is a straightforward string replacement, today it is a mess adding all those parentheses and possibly converting >>stream style syntax. 3- Having special syntax for print puts up a much larger barrier for evolution, e.g. a hypothetical new printf() function is not too far fetched when it will coexist with a print() function. 4- There's no easy way to convert print statements into another call if one needs a different separator, not spaces, or none at all. Also, there's no easy way at all to conveniently print objects with some other separator than a space. 5- If print() is a function, it would be much easier to replace it within one module (justĀ def print(*args):...)Ā or even throughout a program (e.g. by putting a different function inĀ __builtin__.print). As it is, one can do this by writing a class with a write() method and assigning that to sys.stdout -- that's not bad, but definitely a much larger conceptual leap, and it works at a different level than print. You can useĀ print()Ā it in places where you can't useĀ print, such as: [print(x) for x in range(10)] Source : https://www.python.org/dev/peps/pep-3105/
6th Nov 2016, 6:13 PM
Shady Alset
Shady Alset - avatar
0
Simply, it is syntax of python Maybe, used for better understanding
27th Oct 2017, 3:26 PM
#RahulVerma
#RahulVerma - avatar