On Sun, 09 Jul 2006 21:31:00 -0700, placid wrote:

> 
> greenflame wrote:
>> I want to make a function that does the following. I will call it
>> thefunc for short.
>>
>> >>> s = "Char"
>> >>> thefunc(s)
>> >>> s
>> '||Char>>'
>>
>> I tried the following
>>
>> def thefunc(s):
>>     s = "||" + s + ">>"
>>
>> The problem is that if I look at the string after I apply the function
>> to it, it is not modified. I realized that I am having issues with the
>> scope of the variables. The string in the function, s, is local to the
>> function and thus I am not changing the string that was inputed, but a
>> copy. I cannot seem to figure out how to get what I want done. Thank
>> you for your time.
> 
> quick hack
> 
> def thefunc(s):
>     return s = "||" + s + ">>"
> 
>>>> s = "hello"
>>>> s = thefunc(s)
>>>> print s
>        ||hello>>

That's not a quick hack. That's the right way to solve the problem.

Of course, another right way would be to have mutable strings in Python.
I understand why strings need to be immutable in order to work with dicts,
but is there any reason why (hypothetical) mutable strings should be
avoided in situations where they aren't needed as dictionary keys? Python
has mutable lists and immutable tuples, mutable sets and immutable frozen
sets, but no mutable string type.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to