Modify a string's value

s0suk3 at gmail.com s0suk3 at gmail.com
Wed Jul 16 06:55:10 EDT 2008


On Jul 15, 11:55 pm, Ben Finney <bignose+hates-s... at benfinney.id.au>
wrote:
> s0s... at gmail.com 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.
>
<snip>

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