[Tutor] ".=" in Python ?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon May 5 17:46:18 2003


> Sorry I wasn't clear in my previous post(obviouslly, subject too)...
>
> perl:
>	my $a .= $a."text";
> 	print $a;



By the way, the code above should start raising havoc if Perl is in 'full
warnings' mode:

###
[dyoo@tesuque dyoo]$ perl -w
use strict;
my $a .= $a . "text";
print $a;

        [I pressed Ctrl-D at this point to tell Perl to start processing]

Name "main::a" used only once: possible typo at - line 2.
text
###

So in that sense, Perl is trying to accept something that it knows is
incorrect.  Dunno if that's a good thing or not, but that's how Perl
works.




>I thought I did something unnatural so I needed to say 1st line 'a = ""'.
>Or Does it work like this?


You did it right.

    a = ""
    a += "test"
    print a


is fine; it initializes 'a' to the empty string first, and then starts
doing things with it.


Good luck!