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: 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> <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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Thu, May 28, 2015 at 3:29 PM, Marko Rauhamaa wrote: > Ben Finney : > >> It seems the existing =E2=80=98open=E2=80=99 implementation doesn't allo= w 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 =3D MyFile > > Also, in other modules, you can: > > from myfile import open Well, you can... >>> import builtins >>> builtins.open >>> builtins.open =3D "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 =3D open("/tmp/dummy", "w", encoding=3D"utf-8") >>> f <_io.TextIOWrapper name=3D'/tmp/dummy' mode=3D'w' encoding=3D'utf-8'> >>> f.__class__ >>> f.__class__ =3D 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 =3D open >>> def open(*args, **kw): ... cls =3D kw.pop("use_class", None) ... f =3D _orig_open(*args, **kw) ... if cls is not None: f.__class__ =3D cls ... return f ... >>> f =3D open("/tmp/dummy", "w", encoding=3D"utf-8", use_class=3DMyFile) >>> f <_io.TextIOWrapper name=3D'/tmp/dummy' mode=3D'w' encoding=3D'utf-8'> >>> f.demo() Hello, world! But again, I'm really not sure this is a good way to do things. ChrisA