[Discuss] A question for the Python gurus

Deryk Barker dbarker at camosun.bc.ca
Fri Jul 28 20:46:56 PDT 2006


Adam Parkin wrote:
..........

> 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?

Actually, if all you want to do is remove the trailing newline, why not 
simply:

    string = string[:-1]

to slice it off?

(Hmm, will this work in windoze? well, if not why not simply use the 
rstrip assignment instead of creating a function whose only purpose is 
to call rstrip?)




More information about the Discuss mailing list