Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber 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 07:45:11 -0400 Organization: IISS Elusive Unicorn Lines: 48 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=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de qWgnVeM9xTOJOD9IXqnygwZRGGlo9xZuAYxXFCtc2Bkg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"r")': 0.09; 'garbage': 0.09; 'message-id:@4ax.com': 0.09; 'propagate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:which': 0.09; 'index': 0.13; 'def': 0.13; '"with"': 0.16; '2016': 0.16; 'in-memory': 0.16; 'interest,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'url:home': 0.18; '(the': 0.22; '%s"': 0.22; 'exceptions': 0.22; "i've": 0.25; 'header:X-Complaints-To:1': 0.26; 'comfortable': 0.27; 'separate': 0.27; 'turns': 0.27; 'function': 0.28; 'print': 0.30; 'work.': 0.30; 'tue,': 0.34; 'file': 0.34; 'handle': 0.34; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us- ascii': 0.37; 'manual': 0.38; "won't": 0.38; 'data': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'close': 0.61; 'subject:The': 0.61; 'avoid': 0.61; 'mar': 0.65; '20,': 0.66; 'here': 0.66; 'gotten': 0.76; 'fin': 0.84; 'dennis': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-79-219-206.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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:105467 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. I've never gotten comfortable with "with" but if one wants to avoid the manual close operation... def fn2str(fileName): with open(fileName, "r") as fin: imstr = fin.read() return imstr >(Suppose you need both the value and its index in the loop? Then the >one-line for above won't work. For example, 'something' is [10,20,30] >and you want to print: > > 0: 10 > 1: 20 > 2: 30 ) > for i, v in enumerate([10, 20, 30]): print "%s: %s" % (i, v) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/