Pythonic way for missing dict keys

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Aug 1 16:23:02 EDT 2007


John J. Lee a écrit :
> 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 do use callable(). Not everyday, for sure, but still often enough to 
have to reimplement it when I'll switch to Py3K.

And while I rarely use it, I'll regret reduce().

>  I
> would agree there will be a large number of programs that contain one
> or two calls to these functions, though.  Certainly has_key will be
> the most common of those listed above (but trivial to fix).  apply
> will be common in old code from the time of Python 1.5.2.

I still use it (in a somewhat deviant way) to define properties:

class MyClass(object):
   def __init__(self, val):
     self.val = val
   @apply
   def val():
     def fget(self):
       return self._val
     def fset(self, val):
       self._val = val


>  execfile is
> perhaps more common that callable (?) 

Never used it, never saw it used.

> but again is really a "maybe 1
> call in a big program" sort of thing.  Anybody using coerce or reload
> deserves to lose ;-)
> 
> 
> John



More information about the Python-list mailing list