[Edu-sig] Using try / except: any stipulations against routine use?

Vern Ceder vceder at gmail.com
Mon Dec 19 23:27:13 CET 2011


On Mon, Dec 19, 2011 at 4:19 PM, Carl Cerecke <carl at free.org.nz> wrote:

> I know I'm late to this discussion, but I tend to use something like:
>
> if ext not in res_dict:
>     res_dict[ext] = 0
>
> res_dict[ext] += 1
>
> Cheers,
> Carl.
>
>
Hi Carl,

Actually, I often do that as well. I'm not sure it's the most efficient,
but it does make quite clear what you are doing.

Cheers,
Vern



> On 16 December 2011 07:18, Christian Mascher <christian.mascher at gmx.de>wrote:
>
>>
>>  On the other hand,
>>>>     res_dict[ext] = res_dict.get(ext, 0) + 1
>>>>
>>>>  Isn't this at least as readable, and conceptually simpler?
>>>
>>>    if ext in res_dict:
>>>        res_dict[ext] += 1
>>>    else:         res_dict[ext] = 1
>>>
>>>
>>
>> I haven't done much coding in Python lately. And I found this much more
>> readable than the .get method which I forgot about. I positively didn't
>> understand the oneliner just by reading it over and over again.
>>
>> Of course I could easily have opened idle and looked up the .get method
>> online (or looked in a manual).
>>
>> The really good thing about Python is that so much of the core language
>> doesn't surprise you. One is able to write code which is understandable
>> even to people with very little experience or to yourself after not coding
>> in Python for years. Nobody has to write such clear-cut code all the time,
>> but at least it is possible with very little effort.
>>
>> Christian
>>
>> ______________________________**_________________
>> Edu-sig mailing list
>> Edu-sig at python.org
>> http://mail.python.org/**mailman/listinfo/edu-sig<http://mail.python.org/mailman/listinfo/edu-sig>
>>
>
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>
>


-- 
Vern Ceder
vceder at gmail.com, vceder at dogsinmotion.com
The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20111219/3bb3b4f4/attachment.html>


More information about the Edu-sig mailing list