Why no warnings when re-assigning builtin names?

Ethan Furman ethan at stoneleaf.us
Wed Aug 17 19:36:12 CEST 2011

Seebs wrote:
> On 2011-08-17, Steven D'Aprano wrote:
>> On Wed, 17 Aug 2011 01:17 pm Seebs wrote:
>>> Hmm.  See, I've never reached that, in Python or any other language.  I
>>> figure it creates a new potential for confusion, and that I would rather
>>> avoid any ambiguity.  I don't *like* ambiguity in code.
>> Ah, well you see the thing is, this is Python. As soon as you call any
>> function you don't control, you no longer know what your environment is
>> with any certainty. For all you know, the harmless-looking function is
>> monkey-patching builtins like there's no tomorrow. Speaking broadly,
>> perhaps too broadly, we're incredibly proud of the ability to modify nearly
>> everything at runtime.
> Heh.
>> Fortunately, while we are proud of having that ability, actually *using* it
>> is considered a mortal sin. We're not Ruby developers -- if you actually
>> monkey-patch something, especially built-ins, you can expect to be taken
>> outside and slapped around with a fish if you get caught.
> Okay, so.
> Here's what I don't get.
> If it's such a bad thing, *why is it allowed*?  Why are you proud of the
> ability to do something that you are never socially-allowed to do?

Monkey-patching built-ins would be something along the lines of

     import sys
     sys.modules['__builtin__'].str = my_super_string

and is what stands you in jeopardy of being fish-slapped.  ;)

Merely shadowing a built-in, or stdlib, or whatever, isn't monkey-patching.


More information about the Python-list mailing list