<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
On 08/16/2010 12:06 AM, Alan wrote:
<blockquote
 cite="mid:AANLkTimdiy6t8J6w-+kC7CYyq3MNGj9EzXyndEiWMrJU@mail.gmail.com"
 type="cite">Hello,
  <div><br>
  </div>
  <div>I am using things like:</div>
  <div><span class="Apple-style-span"
 style="font-family: sans-serif; font-size: 16px;">
  <pre
 style="border-style: solid none; border-top: 1px solid rgb(170, 204, 153); border-bottom: 1px solid rgb(170, 204, 153); padding: 5px; overflow: auto; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 14px;">
<span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">except</span> <span
 class="ne" style="color: rgb(0, 112, 32);">Exception</span> <span
 class="k" style="color: rgb(0, 112, 32); font-weight: bold;">as</span> <span
 class="n">inst</span><span class="p">:</span></pre>
  </span>and</div>
</blockquote>
<br>
The old syntax for exceptions still works in Python2.x (all versions). 
The new syntax works in Python2.6+  and Python3. <br>
<br>
    try:<br>
        whatever<br>
    except Exception,msg:  # Old syntax<br>
        print msg<br>
<br>
<br>
<blockquote
 cite="mid:AANLkTimdiy6t8J6w-+kC7CYyq3MNGj9EzXyndEiWMrJU@mail.gmail.com"
 type="cite">
  <div><br>
  </div>
  <div><br>
  </div>
  <div><span class="Apple-style-span"
 style="font-family: sans-serif; font-size: 16px;">
  <div class="highlight-python">
  <div class="highlight" style="background-color: rgb(251, 229, 78);">
  <pre
 style="border-style: solid none; border-top: 1px solid rgb(170, 204, 153); border-bottom: 1px solid rgb(170, 204, 153); padding: 5px; overflow: auto; background-color: rgb(238, 255, 204); color: rgb(51, 51, 51); line-height: 14px;">
<span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">with</span> <span
 class="nb" style="color: rgb(0, 112, 32);">open</span><span class="p">(</span><span
 class="s" style="color: rgb(64, 112, 160);">"myfile.txt"</span><span
 class="p">)</span> <span class="k"
 style="color: rgb(0, 112, 32); font-weight: bold;">as</span> <span
 class="n">f</span><span class="p">:</span>
    <span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">for</span> <span
 class="n">line</span> <span class="ow"
 style="color: rgb(0, 112, 32); font-weight: bold;">in</span> <span
 class="n">f</span><span class="p">:</span>
        <span class="k"
 style="color: rgb(0, 112, 32); font-weight: bold;">print</span> <span
 class="n">line</span></pre>
  </div>
  </div>
  </span></div>
</blockquote>
<br>
<br>
You don't need the new fangled with statement if you want to be
compatible with older versions of Python2.  (It's nice and convenient,
but not necessary.)<br>
<br>
f = open(<span class="s" style="color: rgb(64, 112, 160);">"myfile.txt"</span><span
 class="p">)</span><span class="Apple-style-span"
 style="font-family: sans-serif; font-size: 16px;"></span><br>
for line in f:<br>
  print line<br>
f.close()   # This is what the "with" statement guarantees; so now just
do it yourself<br>
<br>
<br>
Gary Herron<br>
<br>
<br>
<br>
<blockquote
 cite="mid:AANLkTimdiy6t8J6w-+kC7CYyq3MNGj9EzXyndEiWMrJU@mail.gmail.com"
 type="cite">
  <div>
  <div>Things that seems to be new in python 2.6 and higher, however
reading <a moz-do-not-send="true"
 href="http://docs.python.org/tutorial/errors.html">http://docs.python.org/tutorial/errors.html</a>
and this not clear when this new syntaxes appeared.</div>
  <div><br>
  </div>
  <div>My trouble is that I want to make something similar above
compatible with python 2.5, but when running python2.5 I got:</div>
  <div><br>
  </div>
  <div>except Exception as msg:</div>
  <div>                      ^</div>
  <div>SyntaxError: invalid syntax</div>
  <div> </div>
  <div>If there's a way (e.g. from __future__ ..., inherited modified
Exception class, etc) that could give a solution to keep my code in
python 2.6 syntax as above but compatible with python 2.5, that would
be appreciated.</div>
  <div><br>
  </div>
  <div>Many thanks in advance,</div>
  <div><br>
  </div>
  <div>Alan</div>
-- <br>
Alan Wilter S. da Silva, D.Sc. - CCPN Research Associate<br>
Department of Biochemistry, University of Cambridge. <br>
80 Tennis Court Road, Cambridge CB2 1GA, UK.<br>
>><a moz-do-not-send="true"
 href="http://www.bio.cam.ac.uk/%7Eawd28">http://www.bio.cam.ac.uk/~awd28</a><<<br>
  </div>
</blockquote>
<br>
</body>
</html>