The rap against "while True:" loops

Aahz aahz at pythoncraft.com
Sat Oct 17 22:27:03 CEST 2009


In article <7xmy3peq3s.fsf at ruckus.brouhaha.com>,
Paul Rubin  <http://phr.cx@NOSPAM.invalid> wrote:
>aahz at pythoncraft.com (Aahz) writes:
>>
>> Standard Python idiom:
>> 
>> try:
>>     d[key] += value
>> except KeyError:
>>     d[key] = value
>> 
>> Maybe you need to re-think "appropriate".
>
>But more recent style prefers:
>
>   d = collections.defaultdict(int)
>   ...
>   d[key] += value

That was a trivial example; non-trivial examples not addressed by
defaultdict are left as an exercise for the reader.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"To me vi is Zen.  To use vi is to practice zen.  Every command is a
koan.  Profound to the user, unintelligible to the uninitiated.  You
discover truth everytime you use it."  --reddy at lion.austin.ibm.com



More information about the Python-list mailing list