Writings of a techie wizard
|
Indexes
Categories
Tags
computers (35)
economics (5) education (3) fantasy (3) history (5) info (8) movies (3) politics (40) science (8) Archives
2015‑Jan (1)
2014‑Sep (2) 2014‑Jul (1) 2014‑Jun (1) 2014‑May (1) 2014‑Apr (2) 2014‑Mar (2) 2014‑Jan (1) 2013‑Nov (1) 2013‑Oct (1) 2013‑Sep (2) 2013‑Aug (2) 2013‑Jun (1) 2013‑Apr (1) 2013‑Mar (1) 2013‑Feb (1) 2013‑Jan (2) 2012‑Dec (3) 2012‑Nov (2) 2012‑Oct (1) 2012‑Sep (3) 2012‑Aug (1) 2012‑Jul (1) 2012‑Jun (1) 2012‑Apr (2) 2012‑Mar (2) 2012‑Feb (1) 2012‑Jan (2) 2011‑Dec (1) 2011‑Nov (3) 2011‑Oct (2) 2011‑Sep (4) 2011‑Aug (6) 2011‑Jul (7) 2011‑Jun (8) |
Single Entry
Tue, 25 Sep 2012
Some time ago I posted about Go vs. Python with regard to delimiters. I now have another reason to prefer Python to Go: Go's error handling (hat tip: Hacker News). To briefly see the issue, consider the following snippet of idiomatic Python: with open(filename, 'r') as f: # do something with f What happens if the try: with open(filename, 'r') as f: # do something with f except IOError: # handle failure to open the file Now consider the corresponding snippet of Go, taken from the blog post linked to above: f, err := os.Open(filename) if err != nil { // handle failure to open the file } // do something with f Two things jump out at me by comparing the above snippets (leaving aside all the stuff about delimiters, etc. that I ranted about last time). First, if the file open fails, Python guarantees that the "do something with f" code will not execute; Go depends on the programmer putting something in the "handle failure to open file" code that does that. Of course, fixing that particular wart is easy: f, err := os.Open(filename) if err != nil { // handle failure to open the file } else { // do something with f } Which of course begs the question, why didn't the blog post write it that way? Perhaps because the poster expected the "do something with f" code to test for a valid file object? In other words, they really intended to write this: f, err := os.Open(filename) if err != nil { // handle failure to open the file } if f != nil { // do something with f } (I'm assuming that testing for a non-nil This by itself may not be a huge issue; but now consider the second thing
that jumped out at me. In Python, I only need to wrap the Why might you not want to? Suppose I'm writing a library to open and parse a particular type of file. This library might be used by a variety of applications; some might be end-user apps for editing the file, while others might be server-side apps that just want a parsed object they can use to read attributes from. The way that a failure to open the file should be handled is very different for these two types of apps: the end-user app needs to display a message to the user (at least if it wants to be usable), while the server-side app probably should just log the error and go on, or perhaps send an urgent page to a sysadmin. If I'm writing this library in Python, handling all this is simple, because
error handling is uncoupled from normal functionality. Each app's code simply
catches the So once again, while it's great that people are trying new things with programming languages, I'm still sticking with Python. |
Wizard Projects
Site Links
Open Source Projects
Old Open Source Projects
Python Recipes
Fun Stuff
Shameless Plugs
Copyright © 2011-2015
by Peter A. Donis All Rights Reserved |