modifying string objects

Uwe Mayer merkosh at hadiko.de
Thu Jun 13 17:40:02 CEST 2002


[This followup was posted to comp.lang.python and a copy was sent to the 
cited author.]

hi,

strings are immutable in python. so what do i do if i need to pass a 
function a string as parameter and need it to be modified within the 
function so that changes are reflected outside of the function body?

f.e. a class constructor must return None

class foobar:
  def __init__(text):
    text = text[-1]			#this is what i actually want

however:

teststr = 'dummy'
foobar(teststr)
print(teststr)

prints "dummy" to the screen.

i _could_ use the globals() function in the object. how 'bad' is this 
style considered to be? can i do it or will i be flamed by other python 
programmers afterwards? ;-)


another question. from perl i konw that f.e.

foo(text = 'some value')

works in the way that first "text" is assigned 'some value' which is 
then passed to the function "foo". i know that the above syntax is used 
to assign the named parameter "text" the value 'some value' and if there 
is no such parameter an error message is produce by the interpreter, but 
why do such language constructs not work in python?
another example:

if (value = myFile.read(10)) == '': ...

?

Thanks in advance 
Uwe



More information about the Python-list mailing list