Help resolve a syntax error on 'as' keyword (python 2.5)
Hi, all. All I'm trying to do is to print the error message using the following code (copying/pasting from IDLE). def div(a,b): print a/b try: div(5,0) except Exception as msg: print msg but IDLE says (while highlighting the 'as' keyword) except Exception as msg: SyntaxError: invalid syntax I've searched the internet and I'm not sure what can cause this. Any help is highly appreciated. I'm using Python 2.5 on Windows XP. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help resolve a syntax error on 'as' keyword (python 2.5)
Hi, except Exception as variable is a new python-3 syntax. You should use except Exception, variable syntax in 2.x series. Vladimir Ignatov On Tue, Nov 3, 2009 at 4:06 PM, Oltmans rolf.oltm...@gmail.com wrote: Hi, all. All I'm trying to do is to print the error message using the following code (copying/pasting from IDLE). def div(a,b): print a/b try: div(5,0) except Exception as msg: print msg but IDLE says (while highlighting the 'as' keyword) except Exception as msg: SyntaxError: invalid syntax I've searched the internet and I'm not sure what can cause this. Any help is highly appreciated. I'm using Python 2.5 on Windows XP. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help resolve a syntax error on 'as' keyword (python 2.5)
I don't have Python 25 on my computer, but since codepad.org is using Python 2.5.1, I did a quick test: # input try: raise Exception(Mrraa!) except Exception as msg: print msg # output t.py:3: Warning: 'as' will become a reserved keyword in Python 2.6 Line 3 except Exception as msg: ^ SyntaxError: invalid syntax # Well that's interesting. as isn't a keyword yet in Python 2.5. This works though, and I think it fits the specification in the documentation. # input try: raise Exception(Mrraa!) except Exception, msg: # Notice it's a comma print msg # output Mrraa! ## Hope that helps, Xav -- http://mail.python.org/mailman/listinfo/python-list
Re: Help resolve a syntax error on 'as' keyword (python 2.5)
Vladimir Ignatov wrote: Hi, except Exception as variable is a new python-3 syntax. You should use except Exception, variable syntax in 2.x series. Not entirely true. This feature has been backported to python2.6 as well. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Help resolve a syntax error on 'as' keyword (python 2.5)
Oltmans rolf.oltm...@gmail.com writes: try: div(5,0) except Exception as msg: print msg The name ‘msg’ here is misleading. The except syntax does *not* bind the target to a message object, it binds the target to an exception object. It would be clearer to write the above code as: try: div(5, 0) except Exception as exc: print(exc) since the target ‘exc’ names an exception object, not a message. (The ‘print’ function will *create* a string object from the exception object, use the string, then discard it.) but IDLE says (while highlighting the 'as' keyword) except Exception as msg: SyntaxError: invalid syntax I've searched the internet and I'm not sure what can cause this. When you get a syntax error, you should check the syntax documentation for the version of Python you're using. Any help is highly appreciated. I'm using Python 2.5 on Windows XP. The syntax above is for Python 3 only URL:http://docs.python.org/3.1/reference/compound_stmts.html#try. In Python 2.5.4, the ‘try … except’ syntax was different URL:http://www.python.org/doc/2.5.4/ref/try.html, and ‘print’ was not implemented as a function, but instead as a keyword URL:http://www.python.org/doc/2.5.4/ref/print.html. Use this form: try: div(5, 0) except Exception, exc: print exc -- \ “When I get new information, I change my position. What, sir, | `\ do you do with new information?” —John Maynard Keynes | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Help resolve a syntax error on 'as' keyword (python 2.5)
En Tue, 03 Nov 2009 10:06:24 -0300, Oltmans rolf.oltm...@gmail.com escribió: Hi, all. All I'm trying to do is to print the error message using the following code (copying/pasting from IDLE). try: div(5,0) except Exception as msg: print msg SyntaxError: invalid syntax I'm using Python 2.5 on Windows XP. Other people already told you what the problem is. I suggest reading a book/tutorial written for the *same* Python version you're using (2.x; it doesn't matter 2.6, 2.5, 2.4...). Once you know the basics of the language, you may look at the differences in the What's new? document for Python 3.0 - but right now, they will just confuse you. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list