Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91351
| X-FeedAbuse | http://nntpfeed.proxad.net/abuse.pl feeded by 78.192.65.63 |
|---|---|
| Path | csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!news.muarf.org!news.roellig-ltd.de!open-news-network.org!border2.nntp.ams1.giganews.com!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'none:': 0.05; 'builtin': 0.07; 'none)': 0.07; 'override': 0.07; 'returned.': 0.07; 'subject:file': 0.07; 'builtins': 0.09; 'open()': 0.09; 'cc:addr:python-list': 0.10; 'question.': 0.13; 'def': 0.14; 'things.': 0.15; 'thu,': 0.15; '"w",': 0.16; 'brutal': 0.16; "can't.": 0.16; 'cls': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function"': 0.16; 'guessing': 0.16; 'modules,': 0.16; 'namespace.': 0.16; 'subject:object': 0.16; 'wanted.': 0.16; 'world!': 0.16; 'world!")': 0.16; 'wrote:': 0.16; 'module,': 0.18; 'hack': 0.18; '>>>': 0.20; 'work,': 0.21; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'assign': 0.22; '2015': 0.23; 'replacing': 0.23; 'this:': 0.23; 'import': 0.24; 'seems': 0.24; 'header:In-Reply-To:1': 0.24; 'idea': 0.26; 'question': 0.26; 'separate': 0.27; 'message- id:@mail.gmail.com': 0.28; "doesn't": 0.28; "i'm": 0.29; 'appear': 0.29; 'function': 0.30; 'work.': 0.30; 'skip:_ 10': 0.32; 'though,': 0.32; 'class': 0.33; 'steven': 0.33; 'open': 0.33; 'received:google.com': 0.34; 'could': 0.35; 'replace': 0.35; 'something': 0.35; 'really': 0.35; 'but': 0.36; 'subject:: ': 0.37; 'is,': 0.38; 'pm,': 0.39; 'does': 0.39; 'sure': 0.40; 'subject: (': 0.40; 'hello,': 0.40; 'your': 0.60; 'skip:u 10': 0.62; 'within': 0.64; '**kw)': 0.84; '**kw):': 0.84; 'can...': 0.84; 'can:': 0.84; 'chrisa': 0.84; 'to:none': 0.90; 'do:': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=OU6oXOD0WA39esLUwLo7rN0m1sFuwQ7ghlIfl9BKNa4=; b=o5hAszjRm92Si7xWqTNWU2Xx18aLgzIH9z+pIHVsKeD8TRoFTjpeMaIftlVvOHpbPI +gRtQRAiJyIPIuZiOw1W00Pm6l8x8CgULEVHMDp/HoVGCUgq5/EFuHviiYGkBv8OEFW1 YSMDC2XCUdpB+J0Datsr8QBgO5Ta2I8MQ+P9ZagxLOK1drv51QQOzW42SzvbwAOLeGJ4 I175by7sy+53k4os9g76iaQUFbh2SuN1oJvRLOlTYDVymwyqhLg5+UOBKQeHN1r+lPQN GhzcjTbk8Ely67dqenxWChu2PtZ+1V+VlVrKEag5Cw4da/60hKecpmlfG7ffptvcNvOw 0xdQ== |
| MIME-Version | 1.0 |
| X-Received | by 10.43.0.67 with SMTP id nl3mr7430083icb.59.1432792159645; Wed, 27 May 2015 22:49:19 -0700 (PDT) |
| In-Reply-To | <87wpztclsu.fsf@elektro.pacujo.net> |
| References | <55667a6d$0$13002$c3e8da3$5496439d@news.astraweb.com> <85iobdwhti.fsf@benfinney.id.au> <mailman.113.1432780860.5151.python-list@python.org> <87wpztclsu.fsf@elektro.pacujo.net> |
| Date | Thu, 28 May 2015 15:49:19 +1000 |
| Subject | Re: Returning a custom file object (Python 3) |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.119.1432792558.5151.python-list@python.org> (permalink) |
| Lines | 66 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1432792558 news.xs4all.nl 2861 [2001:888:2000:d::a6]:52382 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:91351 |
Show key headers only | View raw
On Thu, May 28, 2015 at 3:29 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Ben Finney <ben+python@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Returning a custom file object (Python 3) Steven D'Aprano <steve@pearwood.info> - 2015-05-28 12:16 +1000
Re: Returning a custom file object (Python 3) Ben Finney <ben+python@benfinney.id.au> - 2015-05-28 12:40 +1000
Re: Returning a custom file object (Python 3) Marko Rauhamaa <marko@pacujo.net> - 2015-05-28 08:29 +0300
Re: Returning a custom file object (Python 3) Chris Angelico <rosuav@gmail.com> - 2015-05-28 15:49 +1000
Re: Returning a custom file object (Python 3) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-28 17:04 +1000
Re: Returning a custom file object (Python 3) Chris Angelico <rosuav@gmail.com> - 2015-05-28 19:06 +1000
Re: Returning a custom file object (Python 3) Gary Herron <gherron@digipen.edu> - 2015-05-27 22:56 -0700
Re: Returning a custom file object (Python 3) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-28 16:52 +1000
Re: Returning a custom file object (Python 3) Ben Finney <ben+python@benfinney.id.au> - 2015-05-28 12:34 +1000
Re: Returning a custom file object (Python 3) Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-05-28 10:29 +0100
csiph-web