Modify a string's value

s0suk3 at s0suk3 at
Wed Jul 16 12:55:10 CEST 2008

On Jul 15, 11:55 pm, Ben Finney <bignose+hates-s... at>
> s0s... at writes:
> > I just came across this unusual situation where I'd like to modify a
> > string passed to a function
> Again: Why? The normal way to do this is to create a new string and
> return that.

Yes, usually, but that won't work in this case (look at my previous
post around the 'cin >> line' line). This is what I came up with:

class StreamLineBuffer:
    def __init__(self):
            self.buf = ""

    def __rrshift__(self, stream):
            self.buf = stream.readline()

    def __str__(self):
            return self.buf

cin = sys.stdin
buf = StreamLineBuffer()

cin >> buf
line = str(buf)

Works like a charm :)

More information about the Python-list mailing list