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?
Comments
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.
Then illume points out that it might be help that the shell can provide, and not even be part of the language.
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!")
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.
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.
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).
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. ;)
-Larz
back to the stable version I go. Yay.
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.