Safe to import __builtin__ ?

Robert Kern robert.kern at gmail.com
Thu Jun 4 01:11:51 CEST 2009


On 2009-06-03 17:54, Steven D'Aprano wrote:
> On Wed, 03 Jun 2009 18:57:29 +0000, Benjamin Peterson wrote:
>
>> mrstevegross<mrstevegross<at>  gmail.com>  writes:
>>
>>
>>> Is it generally safe to explicitly import __builtin__ in python? That
>>> is, my code reads like this:
>> ...
>>> It seems like it should be a safe import, but I just want to make sure.
>> Yes, that's fine. I'm not sure why you don't just use type(), though.
>
> I'm not sure why you think type() is a substitute for __builtin__. Here's
> a typical use-case for __builtin__:
>
>
> import __builtin__
> def map(*args):  # shadow a built-in
>      # pre-processing goes here
>      result = __builtin__.map(*args)
>      # post-processing goes here
>      return result
>
>
> How does type() help you?

It doesn't in that sense. The example that Benjamin elided used 
__builtin__.type() for no discernible reason (unlike your example which creates 
a shadow function that overrides the builtin version). Benjamin was asking what 
the motivation was, since it was not evident.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list