Returning a custom file object (Python 3)
Chris Angelico
rosuav at gmail.com
Thu May 28 01:49:19 EDT 2015
On Thu, May 28, 2015 at 3:29 PM, Marko Rauhamaa <marko at pacujo.net> 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.
>
> Within a module, you can simply do:
>
> open = MyFile
>
> Also, in other modules, you can:
>
> from myfile import open
Well, you can...
>>> import builtins
>>> builtins.open
<built-in function open>
>>> builtins.open = "not your grandfather's open() function"
>>> open
"not your grandfather's open() function"
... but I don't think replacing all of open() is what Steven has in
mind; it's a function that does a lot of work, most of which is what's
wanted.
Depending on how brutal you want to be, though, you _could_ hack around it.
>>> from io import TextIOWrapper
>>> class MyFile(TextIOWrapper):
... def demo(self): print("Hello, world!")
...
>>> f = open("/tmp/dummy", "w", encoding="utf-8")
>>> f
<_io.TextIOWrapper name='/tmp/dummy' mode='w' encoding='utf-8'>
>>> f.__class__
<class '_io.TextIOWrapper'>
>>> f.__class__ = MyFile
>>> f.demo()
Hello, world!
This does appear to work. Whether or not it's a good idea is a
separate question. And of course, you could replace open() with
something like this:
>>> _orig_open = open
>>> def open(*args, **kw):
... cls = kw.pop("use_class", None)
... f = _orig_open(*args, **kw)
... if cls is not None: f.__class__ = cls
... return f
...
>>> f = open("/tmp/dummy", "w", encoding="utf-8", use_class=MyFile)
>>> f
<_io.TextIOWrapper name='/tmp/dummy' mode='w' encoding='utf-8'>
>>> f.demo()
Hello, world!
But again, I'm really not sure this is a good way to do things.
ChrisA
More information about the Python-list
mailing list