[Tutor] Operating in Place

Steve Willoughby steve at alchemy.com
Tue Sep 28 22:27:59 CEST 2010


On 28-Sep-10 13:03, Corey Richardson wrote:
> I hate doing this:
> string = string.lower()
>
> Is there a way to do it without the "string =" part? Thanks.

Depends on the class.  In this specific case, string objects are 
immutable (for some good reasons which are beyond the immediate point), 
so once created, they can't be changed.  They can, of course, be used to 
create new strings, which is what string.lower() is doing.  And that new 
string is then given the name "string" again, replacing the old string 
object (which may still have other names referencing it elsewhere).

If you were wanting to modify a mutable object in-place, Python would be 
happy to oblige.  But not strings.

Sorry :)


More information about the Tutor mailing list