[Tutor] Fwd: working with strings in python3

Marc Tompkins marc.tompkins at gmail.com
Tue Apr 19 19:03:10 CEST 2011


Forgot to send to group.  Grrr.

---------- Forwarded message ----------
From: Marc Tompkins <marc.tompkins at gmail.com>
Date: Tue, Apr 19, 2011 at 10:01 AM
Subject: Re: [Tutor] working with strings in python3
To: Rance Hall <ranceh at gmail.com>


On Tue, Apr 19, 2011 at 6:44 AM, Rance Hall <ranceh at gmail.com> wrote:

>  > 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.
>

No, I wasn't conflating 'basic' and BASIC, nor did I think you were - I was
just using BASIC as the first example I could think of where "message =
message + foo" is the standard way to concatenate strings.  You can't do it
that way in C or Perl or assembler;  it's not recommended in Java for much
the same reason as in Python (
http://www.ibm.com/developerworks/websphere/library/bestpractices/string_concatenation.html);
I don't remember what I used to do in COBOL; the only example that came to
mind was BASIC.  Which is a different language from Python, and different
rules apply, which was the only point I was trying to make.

Variables are variable, that's why we call them variable.
> Constants are constant, and that's why we call them constant.
>
> There's an old programmer's saying: "Constants aren't, and variables
don't."  More to the point, though, Python was designed from the ground up
as an object-oriented language (as opposed to a conventional language with
object support bolted on as an afterthought); both constants and variables
are actually objects, and act differently than they do in a non-OO
language.

> 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.
>

Reread the two parts of your own sentence - "strings have been used as
keys... for a long time" and "a string variable is variable or mutable,
where a keyname can not be".  It's like a Zen koan.  Reconcile those two
concepts and I think you will have grasped the Buddha-nature of Python
strings.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110419/4e663005/attachment.html>


More information about the Tutor mailing list