Returning a custom file object (Python 3)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu May 28 02:52:25 EDT 2015


On Thursday 28 May 2015 15:56, Gary Herron wrote:

> On 05/27/2015 10:29 PM, Marko Rauhamaa wrote:
>> Ben Finney <ben+python at benfinney.id.au>:
>>
>>> It seems the existing ‘open’ implementation doesn't allow you to
>>> override the type of object returned.
>> The question is, can you assign to the builtin namespace. I'm guessing
>> you can't.
> 
> Of course you can.
> 
> Python2:
>  >>> __builtins__.open = "whatever"


Don't use __builtins__ with an "s". That's an implementation of CPython and 
may not exist in the future, and doesn't exist in other implementations.

Instead, you should import __builtin__ with no "s". Confusing? It certainly 
is, which is why Python 3 renamed __builtin__ to builtins.




-- 
Steve




More information about the Python-list mailing list