[Discuss] A question for the Python gurus

Andrew Resch andrewresch at gmail.com
Fri Jul 28 21:38:33 PDT 2006


On Fri, 2006-07-28 at 20:46 -0700, Deryk Barker wrote:
> 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?)
> 

The string may not have '\n' at the end.  I am sure that would work in
Windows as Python is meant to be cross-platform, isn't it?

I am not sure either why one would want to create a new function seeing
as rstrip seems to work just fine.



More information about the Discuss mailing list