[Discuss] A question for the Python gurus
Brian Quinlan
brian at sweetapp.com
Sun Jul 30 12:54:31 PDT 2006
Adam Parkin wrote:
> Which was the original version I was complaining about. =8-p
Nope, your version had extra parens. Do you write a lot of C code that
looks like this:
if (((((((condition))))))) {...
>> Rewrite 2
>> ---------
>>
>> for line in sys.stdin:
>> # now do something with line
>
> I actually tried this, but found that there were two problems:
>
> 1) the body of the loop didn't seem to execute until after EOF was found
That is because of caching. Are you really doing interactive input or
just testing that way?
> 2) for some reason I had to hit EOF twice to get the loop to terminate
Yeah, that's interesting and only true on Windows. The same seems to be
true of cygwin's "cat".
> Hmm, care to elaborate why it's a "bad idea" (not efficient, confusing
> to other programmers, etc)? As it turns out it's also impossible, but
> that's a side issue. =8-p
It's a bad idea because it doesn't fit with the normal practices of the
language.
> If you're going to take
> the functional programming approach and say "side effects are bad" then
> you should be making deep copies of all objects passed to functions,
This would be amazingly stupid. Since state cannot be mutated in a
side-effect-free language, then you may as well pay by reference for
efficiency reasons.
> Fair enough. Now if identical strings are pooled, would that help with
> the following example:
>
> while 1:
> # read a line in, string #1
> line = sys.stdin.readline()
> if not line:
> break
>
> # remove CR from end, string #2
> line = line.rstrip('\n')
>
> # Say capitalize the start of the string, string #3
> line = line.capitalize()
>
> Or would you have to allocate memory & copy three strings each time
> through the loop?
That's exactly what it will have to do.
> Now imagine if you say, want to capitalize every word in the line, then
> the memory/time requirements would explode, would they not? If there
> were n words in the sentance, then you'd have to create n copies of the
> string (or so it would seem to me -- correct me if I'm wrong).
"Explode"? The memory required will be triple the length of the line
(plus overhead) i.e. who cares?
Cheers,
Brian
More information about the Discuss
mailing list