[Discuss] A question for the Python gurus

Adam Parkin pzelnip at telus.net
Fri Jul 28 15:53:51 PDT 2006


Continuing with my "question for the ..... gurus" series of e-mails 
(hehe)....

Due to all the recommendations I've been getting lately about the 
language I've decided to give Python a pretty good whirl, but I have a 
question in regards to function calling semantics.

It's my understanding that everything in Python is passed by value, but 
in every case that value is an object reference (much like when you pass 
an object to a method in Java).  It's also my understanding that as a 
result of this any modifications to an argument passed into a function 
will be seen outside of the function.  And finally, it's also my 
understanding that strings are immutable objects.  So combining those 
facts together, it seems to me that there's no way to write a function 
in Python which takes a string as an argument and modifies that string 
in place.  Is this correct?  Like say I wanted to write a Python 
equivalent to Perl's chomp function, I might try something like:

def chomp (string):
	string = string.rstrip('\n')

but of course this doesn't work, as rstrip creates a new string 
reference which we are assigning to "string" and of course since 
references are passed by value the change isn't seen outside of the 
function.  Any way around this?

Thanks in advance,
--
Adam Parkin
E-mail: pzelnip at telus.net
----------------------
  If one man offers you democracy and another offers you a bag of grain, 
at what stage of starvation will you prefer the grain to the vote?
	-- Bertrand Russell


More information about the Discuss mailing list