The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

Chris Angelico rosuav at gmail.com
Tue Mar 22 07:55:17 EDT 2016


On Tue, Mar 22, 2016 at 10:45 PM, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
> On Tue, 22 Mar 2016 11:05:01 +0000, BartC <bc at freeuk.com> 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



More information about the Python-list mailing list