[Discuss] A question for the Python gurus

Adam Parkin pzelnip at telus.net
Mon Jul 31 14:02:52 PDT 2006


Brian Quinlan wrote:
> I don't think that it is very hard to identify the expression, since it 
> is always:

Not saying it's hard to identify, I just prefer the bracketed form as it 
seems a bit clearer/more natural to me (but that's probably my 
C/Java/Pascal/Every-other-programming-language-which-forces-you-to-put-brackets-around-the-expression 
background talking). =8->

> You are doing interactive input using raw reads of stdin? Maybe you 
> could describe your application. I would probably use the raw_input if 
> the application is really supposed to be interactive.

Exact entire Python script:

#!/usr/bin/python

import sys

print "Enter a line:"
for line in sys.stdin:
         print "you entered '" + line + "'"
         print "Enter a line:"

Sample session:

$ python test.py
Enter a line:
fdalkfjsa <ENTER>
sdaflkdsajf <ENTER>
kdfjdslfksdajf <ENTER><CTRL+D for EOF>
you entered 'fdalkfjsa
'
Enter a line:
you entered 'sdaflkdsajf
'
Enter a line:
you entered 'kdfjdslfksdajf
'
Enter a line:
<CTRL+D for EOF>
$

This was done under Cygwin on a Windows XP box, and python -V reports 
"Python 2.4.3" as the version number.

> What is the length of your line? 100 characters, 1000? So you are 
> incurring ~3K of overhead. Oh no!

Okay, I'll bite and play devils advocate: what if you're processing a 
very large file where each line is 50 MB long, then now you're incurring 
100MB of overhead. =8-p

Thoughts like "lines will never be more than X bytes long" are awfully 
close to "640K will be enough for anybody" and we all remember the 
implications of that assumption in the DOS world right?

I see your point though, that strings are usually small and as a result 
the performance penalty of having to create new strings from old is 
probably negligable.
--
Adam Parkin
E-mail: pzelnip at telus.net
----------------------
Programmers work out a model in their heads of how things work and have 
some trouble dislodging that model once they've tested it and come to 
believe in it.  This prevents them from making big mistakes, such as 
switching to a language that's too limited for their needs, but it also 
significantly slows down the shift to a more powerful way of thinking

	-- Bruce Eckel, Introduction to The Tao of Objects


More information about the Discuss mailing list