[Python-ideas] except expression

Ethan Furman ethan at stoneleaf.us
Wed Feb 19 01:02:00 CET 2014


On 02/18/2014 03:58 PM, MRAB wrote:
> On 2014-02-18 20:55, Chris Angelico wrote:
>> On Wed, Feb 19, 2014 at 3:06 AM, Chris Angelico <rosuav at gmail.com> wrote:
>>> I'm currently working on finding a bunch of examples from the stdlib
>>> that could be translated. Will post them once I get the script sorted
>>> out. As it's currently 3AM, though, that means I need to fry up some
>>> bacon or something before I continue :)
>>
>> Alright, results are in.
>>
>> Script:
>> https://github.com/Rosuav/ExceptExpr/blob/master/find_except_expr.py
>> Output:
>> https://github.com/Rosuav/ExceptExpr/blob/master/candidates.txt
>> Annotated examples:
>> https://github.com/Rosuav/ExceptExpr/blob/master/examples.py
>>
>> The last one is the most readable. I've collected up a bunch of viable
>> candidates for translation. (Note that I'm not advocating going
>> through and editing these files for no other reason. That'd just be
>> code churn. But these are cases where, had this feature existed when
>> that code was written, it could have been taken advantage of.)
>>
> At least one of the examples doesn't need this proposed addition.
>
> "getattr" can accept a default, so:
>
>      func = getattr(self, 'do_' + cmd) except AttributeError: self.default
>
> can be written as:
>
>      func = getattr(self, 'do_' + cmd, self.default)

That depends on whether it's okay to evaluate self.default when it's not needed.

--
~Ethan~


More information about the Python-ideas mailing list