[Python-Dev] PEP 463: Exception-catching expressions

Stephen J. Turnbull stephen at xemacs.org
Sat Feb 22 08:12:27 CET 2014


Steven D'Aprano writes:
 > On Fri, Feb 21, 2014 at 02:04:45PM -0500, Yury Selivanov wrote:
 > 
 > > Inconvenience of dict[] raising KeyError was solved by
 > > introducing the dict.get() method. And I think that
 > > 
 > > dct.get('a', 'b')
 > > 
 > > is 1000 times better than
 > > 
 > > dct['a'] except KeyError: 'b'

Aside from the "no need for 'get'" argument, I like the looks of the
latter better, because dct.get('a', 'b') could be doing anything,
while the syntax in the expression is defined by the language and says
exactly what it's doing, even if read in English (obviously you have
to be a Python programmer to understand that, but you don't need to be
Dutch).

 > I don't think it is better. I think that if we had good 
 > exception-catching syntax, we wouldn't need a get method.
 > 
 > "get" methods do not solve the problem, they are shift it.

Note in support: I originally thought that "get" methods would be more
efficient, but since Nick pointed out that "haveattr" is implemented
by catching the exception (Yikes! LBYL implemented by using EAFP!), I
assume that get methods also are (explicitly or implicitly)
implemented that way.

I'm not a huge fan of the proposal (in my own code there aren't a lot
of potential uses, and I think the colon syntax is a little ugly), but
the "eliminates need for .get()" argument persuades me that the colon
syntax is better than nothing.



More information about the Python-Dev mailing list