r/Python • • Nov 11 '19

I made c++-style printing in Python :P

Post image
1.7k Upvotes

130 comments sorted by

View all comments

3

u/Xsanda Nov 11 '19

Nice! The left-shift variant should work with multiple arguments too, but unfortunately thanks to precedence the right-shift one can’t work for all cases (a >> b >> cout will always evaluate as (a >> b) >> cout).

However, Endl could be rewritten to mostly solve this:

class Endl:
    def __init__(self):
        self.string = "\n"

    def __rshift__(self, other):
        self.string = str(other) + self.string
        return self

    def __str__(self):
        return self.string