Returning a custom file object (Python 3)

Gary Herron gherron at digipen.edu
Thu May 28 01:56:41 EDT 2015


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"
 >>>

Python3:
 >>> import builtins
 >>> builtins.open = "whatever"
 >>>

Of course doing so is like shooting yourself in the foot:  Any 
subsequent pain is your own fault and probably well deserved.

Gary Herron



>
> Within a module, you can simply do:
>
>     open = MyFile
>
> Also, in other modules, you can:
>
>     from myfile import open
>
>
> Marko


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418





More information about the Python-list mailing list