[newbie] A question about lists and strings
Chris Angelico
rosuav at gmail.com
Fri Aug 10 06:40:01 EDT 2012
On Fri, Aug 10, 2012 at 8:31 PM, Mok-Kong Shen
<mok-kong.shen at t-online.de> wrote:
> This means there is no way of modifying a string at the top level
> via a function, excepting through returning a new value and assigning
> that to the string name at the top level. Please again correct me, if
> I am wrong.
Yes, but you can (ab)use a one-element list as a pointer.
>>> foo=["Hello"]
>>> def mutate(ptr):
ptr[0]="World"
>>> mutate(foo)
>>> print(foo[0])
World
But it's probably worth thinking about exactly why you're wanting to
change that string, and what you're really looking to accomplish.
There may well be a better way.
Chris Angelico
More information about the Python-list
mailing list