Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91356
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <oscar.j.benjamin@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'python3': 0.05; 'subject:file': 0.07; 'wrapped': 0.07; '*args):': 0.09; 'open()': 0.09; 'subclass': 0.09; 'python': 0.11; 'def': 0.14; '3.2.': 0.16; 'subject:object': 0.16; 'to:name:python list': 0.16; 'wrote:': 0.16; 'resolved': 0.18; 'thanks.': 0.22; 'pass': 0.22; '2015': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'skip:# 10': 0.27; 'object,': 0.27; 'message-id:@mail.gmail.com': 0.28; "i'd": 0.31; 'skip:_ 10': 0.32; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'file': 0.34; 'received:google.com': 0.34; 'to:addr:python-list': 0.35; 'but': 0.36; 'subject:: ': 0.37; 'skip:g 20': 0.37; 'say': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'easily': 0.39; 'subject: (': 0.40; 'special': 0.72; 'object:': 0.84; 'oscar': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=CpxiZeVkei5hIij+Wyzra/PQRoBHLq1zkQMIAdJKRsM=; b=fSIXWaQHRxpAlQOBbB+9lSZySzBgUaKMhH6F3yZpybtyWEPRGjltSf8dhmRkuWe9zD Pbi038C3phzI3stkquvTAy5ZJQkDCtHgOXiH7Vn1NxmAW/sSTbE+XMf6BvImGqVpvlHC EBlOcWjEdgg5TtmMekSEiu9eOnF+bFeN/8zavcnlArrBhCJhFmzrKznwe+uX/Ip7FDSK yfaSaDYDQcsXaqvlFjDY1z04ctMKdnuatiYeu5EVSWC6YR8Hzigdvl6YvpottphoZRFx VrpCcfSMkR7CKK8lpp63cJoNIex8T04ZHdPPTDPReuiVXvCgnA5YI7xG0/MDKTSmWByo uMHQ== |
| X-Received | by 10.194.118.167 with SMTP id kn7mr3427755wjb.113.1432805388097; Thu, 28 May 2015 02:29:48 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <55667a6d$0$13002$c3e8da3$5496439d@news.astraweb.com> |
| References | <55667a6d$0$13002$c3e8da3$5496439d@news.astraweb.com> |
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Date | Thu, 28 May 2015 10:29:27 +0100 |
| Subject | Re: Returning a custom file object (Python 3) |
| To | Python List <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| 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.122.1432805768.5151.python-list@python.org> (permalink) |
| Lines | 47 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1432805768 news.xs4all.nl 2895 [2001:888:2000:d::a6]:60720 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:91356 |
Show key headers only | View raw
On 28 May 2015 at 03:16, Steven D'Aprano <steve@pearwood.info> wrote:
> I'd like to return a custom file object, say my own subclass. I can easily
> subclass the file object:
>
>
> from io import TextIOWrapper
> class MyFile(TextIOWrapper):
> pass
>
>
> but how do I tell open() to use MyFile?
Does the below do what you want?
#!/usr/bin/env python3
class Wrapper:
def __init__(self, wrapped):
self._wrapped = wrapped
def __getattr__(self, attrname):
return getattr(self._wrapped, attrname)
# Special methods are not resolved by __getattr__
def __iter__(self):
return self._wrapped.__iter__()
def __enter__(self):
return self._wrapped.__enter__()
def __exit__(self, *args):
return self._wrapped.__exit__(*args)
class WrapFile(Wrapper):
def write(self):
return "Writing..."
f = WrapFile(open('wrap.py'))
print(f.readline())
with f:
print(len(list(f)))
print(f.write())
>
> Answers for Python 3, thanks.
Tested on 3.2.
--
Oscar
Back to comp.lang.python | Previous | Next — Previous 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