python 3000 breaks hello world

$ ./python 
Python 3.0a1 (py3k, Sep 1 2007, 14:48:21)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
File "", line 1
print "hello world"
SyntaxError: invalid syntax

Weird. I've been trying to learn this python programming language, but the first command in my lecture notes, the three textbooks I have, and all the online tutorials seem to be wrong.

Anyone know how to get hello world working?

Update: oh it seems that print is a function. ---> ;)

Maybe it should show this in the shell, kind of like what typing help does:

>>> help
Type help() for interactive help, or help(object) for help about object.

>>>print 'hello world'
Type print('hello world') to print a string, or print(object) to print an object.

Has any other major programming language ever broken a program as simple as hello world before?


Paddy3118 said…
If you are learning Python then you should not be using the Alpha release of a new version of Python that is heavily advertised as being allowed to break compatibility with older versions to allow a general tidy-up of the language.

Talk to the person who installed the version of Python about not making the alpha version the default, and get access to version 2.5.1.

Python 3 should not be used by beginners as yet - its going through alpha testing by the community.

- Paddy.
James said…
The first few Python 3.0 alpha releases will be very unstable. Some features are turned off, some unimplemented and some will change due to user feedback. Only people who are familiar with Python *and* constantly follow the Python-3000 mailing list have any hope of keeping up. Please take Paddy's advice to stick to Python 2.5.x for now.
Andrew Dalke said…
Given the blog author's other posts on Python, I suspect the phrase "learning Python" was used more for rhetorical exaggeration. That is, once Python 3K is stable and released, a beginning programmer - who doesn't know the difference between Python 3K and Python <3K - might like some better guidance in the traceback error.

Then illume points out that it might be help that the shell can provide, and not even be part of the language.
Brian Ray said…
I would probably grab the most recent stable release not the alpha release. Otherwise, unless you might actually wish to contribute to in development Python 3000, your wasting your time.
Zentrader said…
And there will be a Python 2.x to 3.0 conversion program(s).
Collin said…
We didn't break Hello World -- you're writing it wrong.
Richard Jones said…
Crikey. All these people berating illume for trying the alpha release are missing the point that this will be an issue in the final Python 3.0 as well.

As Andrew Dalke indicated, the problem is that the canonical "Hello World" program now has to be written differently depending on which version of Python you're using -- and the error you get when you get it wrong might be enough to put off a beginner ("but the tutorial/book says to write 'print "hello world"' and that's not working! Stupid language!")
Brandon Corfman said…
There’s no point in acting all surprised about it. PEP 3000 has been on display in your local planning department on Alpha Centauri for fifty of your Earth years, so you’ve had plenty of time to lodge any formal complaint and it’s far too late to start making a fuss about it now.
illume said…
Brandon: It's not far too late to change it.

However this post is mainly about addressing the problem that does exist - for those people who it will matter most. Those people being the total newbies reading some documentation for python1x - python2.x.

A syntax error with a "print is a function now" type message would also be nice - as well as the extra help message in the shell.

Guido actually got boos at europython when he mentioned print was becoming a function in his talk - so people feel quite strongly about it.

All the print debug people are just going to have to suck it up it seems. OR write a hack which allows nice print.

Something like:
easy_install-3.0 nice_print

Then people who like elegant print can just put something in their personal python directory and get away with using the good print.

I think it'll be one of the most popular modules on the cheeseshop.
Arnar Birgisson said…
Such a module would have to modify Python's syntax, even introducing print as a keyword again. That's possible with PyPy in some cases.. but I don't think it will be with Python 3.

I do agree with people that want print to become a function rather than a statement. On its own, print relies on sys.stdout being something appropriate - which is often not the case on GUI and embedded systems. Statements part of a language system should not depend on the library.

Python 3.0 will be known to break compatibility in other places, so learning Python 3 from an earlier tutorial is likely to break anyway. I'm sure most tutorials will be updated, at least with a note that they apply to pre 3.0 versions.

As for providing a better error message like for help, how do you propose to do that? "help" is an object with a string representation - so typed on it's own on the console, that representation is printed. Type

help "something"

on the console and you'll get a syntax error just as for

print "something"

To provide a "good" error message there as you describe, you would have to catch that special case in the parser, which is an overall bad idea. For one thing you'll be making unnecessary checks for code that is not running in an interactive shell.

The fact remains that NAME LITERAL is a parse error and should be presented just like that. Otherwise we'd have to catch other cases as well, like

>>> str 10
Use str(10) if you want to convert a type to a string.
Anonymous said…
One approach would be to allow:

print 'hello world'

as valid syntax for calling a function (as well as the traditional syntax). Lua allows this (when the argument is a single string or a single table) and it's really great. ML also allows it, in fact it's the only syntax for calling a fuction in ML.

I'd have to sit down properly with the Python syntax to see what grammatical problems it introduces (and it usually does).
Anonymous said…
Here Guido shoots down a similar proposal.
Larz said…
All functions entered this way produce the same obtuse message.

Perhaps, for the interactive shell at least (though this could be nice even at run time, I suppose), offer to show the syntax of the function in question?


>>> len "string"
File "<stdin>", line 1
len "string"
SyntaxError: invalid syntax

"len" is a built-in function

Would you like to see help( len ) ([Y]/N)?


Or, perhaps, simply show the doc string (with "more" as apropos)?

I've long thought that "print" should be a function, just for consistency. (Obviously, I like not needing parens currently, however. ;)

pomo housewife said…
thanks you SO much for posting this, and to your respondents for helpful answers. I thought I must be losing my marbles!

back to the stable version I go. Yay.
xbb1024 said…
its now 2012 and ive just downloaded and installed python 3.2.3., tried print "hello world", and got the SyntaxError message.

Not a big problem, took me less then a minute of googling to find the answer. Also, there was a warning on the download page.

Popular posts from this blog

Is PostgreSQL good enough?

Experiments with new low latency PyPy garbage collector in a thread.

🐱‍🏍 — the first pygame 2 community game. Starting now! Are you in?