3 ms·
Why print as a function? From the PEP[0]: print is the only application-level functionality that has a statement dedicated to it. Within Python’s world, s
by robertlagrant 2y ago
Why print as a function?
From the PEP[0]:
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.
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.
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.
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.
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.
[0] https://peps.python.org/pep-3105 https://peps.python.org/pep-3105
- rich_sasha 2y agoYes, if Python was made from scratch, then perhaps function is better (I don't really mind). But breaking one of the most commonly used statements for what is essentially aesthetics? No thanks. Introduce a function that does the same if you like. That's precisely my point, this syntax change (and many others) was entirely unnecessary, put people off upgrading, then got them told off for being dinosauric luddites. Frankly, it's not even that consistent. Is `del dict[key]` necessary when you can call `dict.pop`? Either way, these would be fine discussions to have, but not when you consider the people upgrading thei codebases.
- robertlagrant 2y ago> Is `del dict[key]` necessary when you can call `dict.pop`? Reason for Python 4 detected!