[Tutor] working with strings in python3

Rance Hall ranceh at gmail.com
Tue Apr 19 15:44:21 CEST 2011


On Mon, Apr 18, 2011 at 9:50 PM, Marc Tompkins <marc.tompkins at gmail.com> wrote:
> On Mon, Apr 18, 2011 at 6:53 PM, Rance Hall <ranceh at gmail.com> wrote:
>
>>
>> I'm going to go ahead and use this format even though it is deprecated
>> and then later when we upgrade it I can fix it.
>>
> And there you have your answer.
>
>> A list might make sense, but printing a message one word at a time
>> doesn't seem to me like much of a time saver.
>
>
> Did you try my example code?  It doesn't "print a message one word at a
> time"; any time you print " ".join(message), you get the whole thing.  Put a
> \n between the quotes, and you get the whole thing on separate lines.
>

I think you misunderstood me, I simply meant that the print "
".join(message) has to parse through each word in order to get any
output, I didn't mean to suggest that you got output one word at a
time.  Sorry for the confusion.

> It's just that it's not stored as a single string, but assembled on-demand.
> And you're right - doing this once or twice, or even ten times as in my
> example code, doesn't make a huge difference.  But at some point, if you
> intend to continue programming, you will run into a situation where your
> code needs to do something over and over and over and over... and getting
> out of bad habits like string concatenation can make a very noticeable
> difference in speed.
>
>>
>> But are CLI apps so rare that this sort of thing just doesn't happen
>> anymore?
>
> No, they're not all that rare, and CLI-ness has nothing to do with it.
>
>>
>> This seems like such a basic thing and deprecating it seems
>> rather harsh.
>>
> Bottom line: Python is not BASIC.  In BASIC, strings aren't immutable, so
> in-place string concatenation doesn't carry (much of) a performance
> penalty.  In Python, it will make your program unnecessarily slow.  I think
> you're under the impression that "deprecation" is a value judgment, or that
> "message = message + foo" is deprecated because it looks like BASIC syntax.
> Neither is true.
>

Again a little misunderstanding.  I didn't mean BASIC the programming
language.  I forgot all I knew about that language long ago.  I mean
basic in the fundamental concept sense.

Variables are variable, that's why we call them variable.
Constants are constant, and that's why we call them constant.

The idea of immutable strings variables blurs the line between these
two mathematical concepts which are part of my thinking about teaching
math (which I do at a local community college) as well as my thinking
of computer programming.

> From the Python wiki:
> http://wiki.python.org/moin/PythonSpeed/PerformanceTips#String_Concatenation
>

Thanks for this pointer, it was interesting reading.

> We may ask ourselves "why did Guido decide to make strings immutable in the
> first place?"; probably the best reason is "so that strings can be used as
> keys in a dictionary", but since I'm not Guido - not even Dutch! - I really
> can't speak for him.
>

I'm not sure I buy this explanation as strings have been used as keys
in dictionaries or "associative arrays" for a long time, a string
variable is variable or mutable, where a keyname can not be.

Anyway, thanks everyone for taking the time to help me get my head
wrapped around this, I'm not sure I managed to do it yet, but the
seeds were planted.

Rance


More information about the Tutor mailing list