[Tutor] working with strings in python3

Rance Hall ranceh at gmail.com
Tue Apr 19 03:53:15 CEST 2011


On Mon, Apr 18, 2011 at 8:10 PM, Marc Tompkins <marc.tompkins at gmail.com> wrote:
> On Mon, Apr 18, 2011 at 5:17 PM, Rance Hall <ranceh at gmail.com> wrote:
>>
>> Ok so I know what I am doing is deprecated (or at least poor form) but
>> the replacement must be awkward cause I'm not getting it.
>>
>>
>> so this is in a cli based program designed to print status messages to
>> the terminal on a linux box.
>>
>> pseudo code:
>>
>>
>> message = "Bah."
>>
>> if test:
>>   message = message + " Humbug!"
>>
>> print(message)
>>
>> end pseudo code
>>
>>
>> I'm sure this is not the way we are supposed to augment strings like this.
>>
>> maybe there is string.append() method or something I should be using
>> instead?
>>
>> In my case the optional extra parts are always at the end of the
>>>
>>> current value of the string.
>>
>
> The reason it's deprecated is because it's SLOW - since strings are
> immutable, there's a lot of extra copying going on behind the scenes.  If
> this code were inside a loop, being run lots and lots of times, this would
> quickly become one of the bottlenecks of your program.  However, in the
> example you've given, I'm not sure it would make an important difference -
> as slow as string concatenation is, it's still a heck of a lot faster than
> the human operator!
>

So you have a CLI based app that asks a question of the user and you
are trying to feed the prompt for the input function.

I have a mode variable that stores how a function was called so I know
the difference between a new entry, and an entry update.

An entry update has default values for all the questions.

so you might have an example like:

message = "Please type the location address line 1."

and if the mode was edit you can add

message = message + "\nPress Enter to accept the default of " +
dbrecord["address1"]

then later your code doesn't care it just prints the message variable
no matter what it contains.

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.

> If you just want to have a status update for your user - like a progress bar
> - you could simply print one string at a time and not store the result.
> (i.e. if you just want to display a line of dots to indicate activity, you
> don't need to store the full number of dots - just print one at a time -
> without newlines - and forget about it.)
>
> If, on the other hand, you always want to have access to the full string AND
> you want to print it along the way, then try a list.  Lists are awesome!
>
>> message = ['Bah'] # message is a list, which currently contains one item:
>> 'Bah'
>> for x in range(10):
>>     message.append(str(x))  # append a string ('0', '1', etc.) to the list
>>     print " ".join(message)  # print the contents of the list, separated
>> by spaces
>
> That's if you want to do this in a loop.  If you have a set number of items
> to display, then string formatting is your friend:
>
>> message = 'The %s in %s stays mainly in the %s' % ('rain', 'Spain',
>> 'plain')
>
>

String formatting doesn't work for me in this case as the message is
sort of dynamic.  Way too much logic around printing a statement.

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.

I totally get the immutable string thing.  I can't say that I care one
way or the other, mutable or not, each has consequences.

But are CLI apps so rare that this sort of thing just doesn't happen
anymore?  This seems like such a basic thing and deprecating it seems
rather harsh.

I'm not really trying to start a flame fest, I'm just trying to
understand the thought process.  I don't see what I would call a
legitimate alternative to this type of string usage.

Rance


More information about the Tutor mailing list