Python 3: print or print()

There’s a thread over on the Python list which started a week ago as I write and is still running. The OP titled his post the provocative “I strongly dislike Python 3″ but what it really comes down to is “I strongly dislike print()”. Various people have come in on both sides of this well-worn argument.

Now, I don’t really mind futile discussions, and this clearly is one: Python 3 isn’t going to switch back, no matter how many people weigh in on the print-as-statement side. I’m in that camp myself, and when the matter was first up for discussion I had a to-and-fro with Alex Martelli on the subject which I backed away from fairly quickly. I continue to enjoy and benefit from Python regardless; I contribute to its development very slightly and very rarely; and even were I the most prolific contributor on the planet, I don’t believe that would give me any particular right to dictate design decisions of this sort. You take the rough with the smooth.

I think the side of the discussion which irks me the most is from those who are defending the print-as-function decision. Clearly there are cogent and persuasive reasons why print should be / should have been a function. No-one’s pretending that this was a decision taken randomly and for the sake of change. Or even for the sake of a foolish consistency. But if someone’s working practice involves using print a lot — perhaps in the interpreter rather than in a code editor — then the switch to the function version is clearly a burden. It may be more or less of a burden, but it involves having to do something which you didn’t have to do before. Being told that you should have been using logging or pdb or sys.stdout.write — all of which are valid tools to use in the proper place — doesn’t really make your life any easier.

So while Python 3 is now the default interpreter on my shortcut keys and I’m trying to write new code against 3.2, I still find it a pain to overcome the very long habit of, eg, print ad.find_user ().sAMAccountName. Obviously if Python 1.5.2 had had print () as a function originally, I wouldn’t find is so hard now. ;)

(As an aside I did wonder at first whether the American or some other non-UK keyboards had the brackets in some more convenient spot, but it doesn’t appear so…)

3 Comments so far »

  1. Safe Hammad said,

    Wrote on July 2, 2010 @ 4:15 pm

    The porting of IPython to Python 3 should provide some welcome relief for those who prefer print-as-statement.

    IPython is “an enhanced interactive Python shell” which, amongst other features, allows the user to call functions without having to use parentheses. This would naturally include the Python 3 print-as-function. Here’s an example:

    In [1]: def add(a, b):
    …: return a + b

    In [2]: add(5, 3)
    Out[2]: 8

    In [3]: add 5, 3
    ——> add(5, 3)
    Out[3]: 8

    I’m not sure how close we are to a Python 3 version of IPython however there are many other excellent (and less dubious) reasons to use IPython as your interactive shell in the meantime e.g. tab completion, editing input history as a python file etc.

  2. OpenID said,

    Wrote on July 3, 2010 @ 11:09 am

    When I learnt of the cchange, it made sense to me. I think I’m being lazy now if I *don’t* use parenthesis in a python 2.6 program! But this may be because of my involvement in where I try and create code examples for Python 3 and Python 2; and where having one example that will work for both versions of Python is neater.

    - Paddy.

  3. Antoine P. said,

    Wrote on July 3, 2010 @ 9:33 pm

    “(As an aside I did wonder at first whether the American or some other non-UK keyboards had the brackets in some more convenient spot, but it doesn’t appear so…)”

    French keyboards do. The underbar is also non-shifted, which is nice with PEP 8 :)

Comment RSS · TrackBack URI

Leave a Comment


Sign in with your OpenID ?


Name: (Required)

E-mail: (Required)