[Tutor] First program

Luke Paireepinart rabidpoobear at gmail.com
Sat Mar 13 21:18:22 CET 2010

On Sat, Mar 13, 2010 at 12:04 PM, Ray Parrish <crp at cmc.net> wrote:

> Luke Paireepinart wrote:
>> Your version creates at least 10 intermediate strings before outputting.
>> Remember strings are immutable in Python. So you're constructing strings
>> The area of a
>> The area of a rectangle
>> The area of a rectangle is
>> 12
>> The area of a rectangle is 12
>> The area of a rectangle is 12 x
>> 10
>> The area of a rectangle is 12 x 10
>> The area of a rectangle is 12 x 10 equals
>> 120
>> The area of a rectangle is 12 x 10 equals 120
>> The area of a rectangle is 12 x 10 equals 120 square feet
>> With string formatting you avoid all of these intermediate strings, so
>> it's arguably more efficient.
>> Other than just viewing from a performance standpoint though, I find it
>> much easier to read my version, because any computation required takes place
>> at the end of the line.
>> For example, your inline str(width*height) requires you to read the whole
>> line to see it.
>> It's really a personal thing, it's easier for me to read the formatting
>> version than the string concatenation version, in most cases.
>> Now if you had used the comma convention I would have seen your point.
>>  This is, I think, the easiest to read of all 3
>> area = width * height
>> print "The area of a", choice, "is", width, "x", height, ", which equals",
>> area, "square feet."
>> Also, why are you capitalizing variable names?  That's a pretty unusual
>> convention.

>  Thanks for letting me know how inefficient my method is. I'll remember
> that, and apply your suggestions to my code from now on. So, you're saying
> that the commas method also does not suffer from the overhead of creating a
> bunch of individual strings?

Yes, the 'comma method' is actually doing something sorta tricky behind the
scenes: it's creating a tuple and passing it to print.
Look what happens when you just comma-separate stuff normally:
>>> 'hello','how','are','you?'
('hello', 'how', 'are', 'you?')

This implicit tuple conversion is useful in other situations too:
>>> a, b = 1 , 2
>>> a
>>> b

this is creating the tuple (1,2) and then iterating over the tuple and
assigning values to whatever's on the left hand side (a, b in this case).
And you can abuse it if you really want to, to force things into tuples:
>>> a,

So what's happening when you call print with the tuple, is that print is
basically doing this behind the scenes:
for item in tuple:
    sys.stdout.write(" ")

Of course it doesn't do this in Python but rather in C, at a lower level,
but that is basically the idea.
If print gets a list or a tuple of strings it will iterate over them and
output them to standard out with spaces.  This doesn't incur the string
concatenation overhead.

Truthfully, the penalty for concatenating strings is not that big, the
reason most people don't use the + method is just because they find it
harder to read/follow.
Or, I should say: the penalty for concatenating strings _that you're going
to output_ is not that big, because you tend to output short strings and not
a lot of them,
otherwise the output is pretty much unreadable.

If you do something like this:
x = ""
for i in range(1000000):
    x += str(i) + " "

You'd probably want to change it to
y = []
for i in range(1000000):
x = " ".join(map(y, str))

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100313/180c72d2/attachment-0001.html>

More information about the Tutor mailing list