Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Tue, 22 Mar 2016 22:55:17 +1100 Lines: 35 Message-ID: References: <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> <56f09973$0$1601$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de lnSMCAheyp1ApkeK5ypvjw0hbWbIyLlfYyRAZuY79wSg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cc:addr:python-list': 0.09; '"r")': 0.09; '22,': 0.09; 'garbage': 0.09; 'propagate': 0.09; 'subject:which': 0.09; 'though...': 0.09; 'def': 0.13; '10:45': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'in-memory': 0.16; 'interest,': 0.16; 'pythonic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(the': 0.22; 'exceptions': 0.22; 'header:In-Reply-To:1': 0.24; 'not.': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'turns': 0.27; 'function': 0.28; "we're": 0.30; 'code': 0.30; 'tue,': 0.34; 'file': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'files,': 0.35; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'data': 0.39; 'takes': 0.39; 'subject:The': 0.61; 'mar': 0.65; 'here': 0.66; 'subject': 0.70; 'chrisa': 0.84; 'fin': 0.84; 'idiomatic': 0.84; 'to:none': 0.91; 'dennis': 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; bh=CRGhN8Bg2T99r4b3ew1hFgsWA52eg8UwTHl1lxxC0Fc=; b=w6dSD8bSNFvKR0jNQKUzhNirf9OAP1tYzxAV5so0YgxQ1hbPV666Hv8glMwHGI4sqZ DwqIL1Yj+D38b+HkhYQygjY1v1WwTp5kbuDTLAd0f2VonwoUz7zoHnLs5E/nh142zDSE kO6vd3ZUdWcwgoz2ejlOwg135nCW+ZAYuMhZrKqSb8IfIzv+cOJY3VfCMQz0/YlUWaQ9 CNntVUW0YxmAK7pA+Y4szXnI6vF79Jm7f468LdMODwQFEhuMZogs0KbbsQjzyCVdXkHq 9LgkOblI1PjvuZWIvXh6+zLJ0bvMpDqTqkHirEY8huUCQ9X+yq83ydI5Y5c9k7YOdqIE gvPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=CRGhN8Bg2T99r4b3ew1hFgsWA52eg8UwTHl1lxxC0Fc=; b=EtJtz/kk9rKbuMVCu8nsey7lzs7n0QGyLEEz06t1YDxTvY2zPqE80fyG+BCHie0tdr wkabG/3jufHCIPisILXuU88femwYsKEoBF0nfBxZb4vOxL77MVjbcJz01eN87o9O4m+e rvZfFt+PHHUdTvZvscMOXAz7JEQqE97+hKJokvUsFogXnIGFlBeApYyQQrivc9zj3xiC CpXr1IYg7x5bIXCQAU8+TBK8YQ+Cy4c4PlTgt9GtxNzeS+iyiOeDjWlrQ4PmvG8829tC 3bXL7iMdnDuzj8b/o7uiTmVkgHhXoRR7mcOpwern2zXMMWRqE0IYphiNYCX5hya+ZPY3 Wrpw== X-Gm-Message-State: AD7BkJL8zoeZGbScED4cu0Qczi3Q6f5ukyc09mzDoFNUZkBMAuoy1zbvWjUZFsR92WyOh9uBMp7r8HbOoR3vkw== X-Received: by 10.112.147.101 with SMTP id tj5mr10397064lbb.119.1458647718195; Tue, 22 Mar 2016 04:55:18 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105468 On Tue, Mar 22, 2016 at 10:45 PM, Dennis Lee Bieber wrote: > On Tue, 22 Mar 2016 11:05:01 +0000, BartC declaimed the > following: > >> >>But out of interest, how would /you/ write a function that takes a >>file-spec and turns it into an in-memory string? And what would its use >>look like? >> > At the basics -- and letting the garbage collector get the file handle > later... > > imstr = open(fileName, "r").read() > > If you want a separate function... (the name here stinks, but...) > > def fn2str(fileName): > fin = open(fileName, "r") > imstr = fin.read() > fin.close() > return imstr > > ... > data = fn2str("some.file") > > letting any exceptions propagate upwards. While we're on the subject of Pythonic ways to read files, this is NOT. http://thedailywtf.com/articles/finding-the-file It's also not idiomatic C# code either, though... ChrisA