C++ style Stream Operators

Rainer Deyke root at rainerdeyke.com
Sun Feb 11 21:53:57 CET 2001


"Alex A. Nikiforov" <nikifor at sky.inp.nsk.su> wrote in message
news:9661s1$1ev8j$2 at sky.inp.nsk.su...
> Alex A. Nikiforov <nikifor at sky.inp.nsk.su> wrote:
> > Rainer Deyke <root at rainerdeyke.com> wrote:
> >> class ostream:
> >>   def __init__(self, buf):
> >>     self.buf = buf
> >>   def __lshift__(self, rhs):
> >>     self.buf.write(str(rhs))
>
> >> cout = ostream(sys.stdout)
> >> cout << 'Hello world!\n'
>
>
> > what about cin ?

Can't be done.  In Python, the expression 'A >> B' cannot assign to 'B'.

> or at least scanf ?

Again, not possible with C-style syntax due to lack of the address-taking
operator ('&').  There might be a function that does something similar
(returning a tuple of values given an input string and a format string); if
there is, I don't know about it.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list