Pythonic way for missing dict keys

Steve Holden steve at holdenweb.com
Sat Jul 28 08:23:21 EDT 2007


Alex Popescu wrote:
> jjl at pobox.com (John J. Lee) wrote in news:87lkd0eprj.fsf at pobox.com:
> 
>> Alex Popescu <nospam.themindstorm at gmail.com> writes:
>>
>>> Zentrader <zentraders at gmail.com> wrote in
>>> news:1185041243.323915.161230 @x40g2000prg.googlegroups.com:
>>>
>>>> On Jul 21, 7:48 am, Duncan Booth <duncan.bo... at invalid.invalid>
>>>> wrote: 
>>>>
>>>> [snip...]
>>>>
>>>>
>>>> >From the 2.6 PEP #361 (looks like dict.has_key is deprecated)
>>>> Python 3.0 compatability: ['compatibility'-->someone should use a
>>>> spell-checker for 'official' releases]
>>>>         - warnings were added for the following builtins which no
>>>> longer exist in 3.0:
>>>>              apply, callable, coerce, dict.has_key, execfile,
>>>>              reduce, 
>>>> reload
>>>>
>>> I see... what that document doesn't describe is the alternatives to
>>> be used. And I see in that list a couple of functions that are
>>> probably used a lot nowadays (callable, reduce, etc.).
>> callable and reduce are rarely used, at least in code I've seen.  
> 
> I thought G would be using that function a lot. Also, what is the 
> replacement of reduce? I think I remember seeing somewhere that lists 
> comprehension would be (but also remember the advise that reduce will be 
> quicker).
> 
See his latest blog entry on artima.com, where he has started a Py3k 
FAQ. He suggests that uses if reduce are almost always more readable 
when written using a for loop and an accumulator variable.

   http://www.artima.com/weblogs/viewpost.jsp?thread=211200

>> Certainly has_key will be
>> the most common of those listed above (but trivial to fix).  
> 
> dict.has_key(key) becomes key in dict (correct?)
> 
>> apply
>> will be common in old code from the time of Python 1.5.2.  
> 
> I think there were some advises to not use apply.
> 
Yup. Anyway there's a trivial translation for uses of apply.

     apply(f, *args, **kw) =>  f(*args, **kw)

The converter will almost certainly do this for you.

>> execfile is
>> perhaps more common that callable (?) but again is really a "maybe 1
>> call in a big program" sort of thing.  
> 
I don't ever remember using execfile. It has always been a blot on the 
landscape.

> What is the replacement for this one? 
> 
I'd have to guess, and will refrain from doing so.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list