code copy from pep-3134 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: code copy from pep-3134 (/thread-12466.html) |
code copy from pep-3134 - bluefrog - Aug-26-2018 I'm attempting to run the snippet of code in PEP-3134, but I'm not getting the errors stated. The code relates to implicit exception handling. The expectation, according to the PEP, is: Quote:The ultimate result will be an AttributeError due to the misspelling of clos, whose __context__ points to a NameError due to the misspelling of ex, whose __context__ points to an IOError due to the file being read-only, whose __context__ points to a ZeroDivisionError, whose __context__ attribute is None. #!/usr/bin/python3 def main(filename): file = open(filename) # oops, forgot the 'w' try: try: compute() except Exception, exc: log(file, exc) finally: file.clos() # oops, misspelled 'close' def compute(): 1/0 def log(file, exc): try: print >>file, exc # oops, file is not writable except: display(exc) def display(exc): print ex # oops, misspelled 'exc' if __name__ == "__main()__": main('t.txt')I get the following syntax error instead of what is expected: Can anybody suggest why ?
RE: code copy from pep-3134 - snippsat - Aug-26-2018 (Aug-26-2018, 02:59 PM)bluefrog Wrote: Can anybody suggest why ?Indentation is wrong on web-site,so have to fix that. Here also a fix so it work Python 3.6 -->. def main(filename): file = open(filename) # oops, forgot the 'w' try: try: compute() except Exception as exc: log(file, exc) finally: file.clos() # oops, misspelled 'close' def compute(): 1/0 def log(file, exc): try: print >>file, exc # oops, file is not writable except: display(exc) def display(exc): print(ex) # oops, misspelled 'exc' main('result.csv')
|