[Python-ideas] except expression
Rob Cliffe
rob.cliffe at btinternet.com
Wed Feb 19 01:28:30 CET 2014
More information about the Python-ideas mailing list
Wed Feb 19 01:28:30 CET 2014
- Previous message: [Python-ideas] except expression
- Next message: [Python-ideas] except expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 18/02/2014 22:56, Nick Coghlan wrote: > > > On 19 Feb 2014 06:56, "Chris Angelico" <rosuav at gmail.com > <mailto:rosuav at gmail.com>> wrote: > > > > My script is currently _very_ simplistic. It looks *only* for > > assignments, so it won't find something like this: > > > > try: > > foo.append(args[0]) > > except IndexError: > > foo.append('') > > > > which is correct, because it's impossible to know whether foo.append() > > will raise IndexError. (Chances are it won't, especially if we know > > foo is a list, for instance.) It's not safe to directly translate that > > sort of thing. It might, however, be something worth improving, as it > > narrows the scope of the except clause. But if that same code is > > written thus: > > > > try: > > tmp = args[0] > > except IndexError: > > tmp = '' > > foo.append(tmp) > > > > then the script _will_ find it, and then it really is a candidate > for editing. > > This example should go in the PEP - the except clause in the example > code is almost certainly too broad, but the fix is rather ugly. > > With the PEP, the except clause can easily be moved inside the > expression so it doesn't cover the append() call: > > foo.append((args[0] except IndexError: "")) > > Cheers, > Nick. > Beautiful catch, Nick! Shows that this proposal not only gives a neater way of writing code without changing the semantics, but may actually point the way to _improving_ existing (or new) code. Rob Cliffe > > > > _______________________________________________ > Python-ideas mailing list > Python-ideas at python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > > > No virus found in this message. > Checked by AVG - www.avg.com <http://www.avg.com> > Version: 2012.0.2247 / Virus Database: 3705/6604 - Release Date: 02/18/14 > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140219/666b45b1/attachment-0001.html>
- Previous message: [Python-ideas] except expression
- Next message: [Python-ideas] except expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list