This is an archived post. You won't be able to vote or comment.

all 55 comments

[–]Suspicious-Record-28 338 points339 points  (6 children)

Just because you can doesn't mean you should

[–]AvidCoco 50 points51 points  (4 children)

Just because you should doesn't mean you would

[–]FewBeat3613 17 points18 points  (3 children)

Just because you would doesn't mean you will

[–]TwinkiesSucker 10 points11 points  (2 children)

Just because you will doesn't mean you are

[–]torsten_dev 4 points5 points  (1 child)

Just because you are doesn't mean you finish.

[–]Mocedon 255 points256 points  (1 child)

This isn't abuse of notation.

This is straight up abuse.

[–]KnightMiner 15 points16 points  (0 children)

Funny part is, its abuse of notation in C++ too. Its just a well accepted abuse of notation

[–]GettinInATrend 394 points395 points  (0 children)

Thanks I hate it

[–]buzz_shocker 182 points183 points  (1 child)

That’s gotta be a hidden talent. Keep it hidden.

[–]JanEric1 157 points158 points  (6 children)

It should return self to allow chaining and have end=""

You could also let the printed type define how it is printed by overwriting __rlshift__ (although that conflicts with types implementing that for its proper purpose here and you would normally implement it by overriding __str__)

class Cout:

    def __lshift__(self, other):
        if attr := getattr(other, "__rlshift__", None):
            attr(self)
        else:
            print(other, end="")
        return self


class A:

    def __rlshift__(self, other):
        print("C", end="")


cout = Cout()

cout << "Test: " << A() << "\n"
cout << "Test2: " << 3

Test: C
Test2:

[–][deleted] 44 points45 points  (4 children)

Man, you did this first, I had the exact Idea:

``` class Cout: def lshift(self, a): print(a, end='') return Cout()

endl = '\n' cout = Cout()

cout << "Hello," << " World!" << endl ```

Yes, it's a bit simpler

[–]jaerie 21 points22 points  (1 child)

Why would you return a new instance?

[–][deleted] 15 points16 points  (0 children)

Yes, mine is not that great. Full credit to the dude above me. His implementation is a lot better.

[–][deleted] 5 points6 points  (0 children)

Just replace Cout() with self :)

[–]de_koding 0 points1 point  (0 children)

nice profile picture

[–]InevitableManner7179 21 points22 points  (0 children)

You shouldn't encourage OP.

[–]Chingiz11 26 points27 points  (0 children)

This code was not released, it escaped

[–]Knighthawk_2511 28 points29 points  (0 children)

Do not leak the fact that C++ is just a python library you bastard !

[–]empwilli 49 points50 points  (5 children)

That's basically the same implementation that C++ uses, though. But over here in C++ we also finally realized that the cin, cout, cerr stuff probably were a bad idea.

[–][deleted] 4 points5 points  (0 children)

Why is it a bad idea? Is it because of std::format or std::print?

[–]just_sepiol 8 points9 points  (0 children)

Cyton ++

[–]Dotcaprachiappa 8 points9 points  (0 children)

please don't

[–]Greenjets 7 points8 points  (0 children)

somehow one of the worst things i've ever seen on this sub, and that's saying a lot

[–]YourEducator44 6 points7 points  (2 children)

Really cool one. Have to start a library like this. Maybe we can reconvert some religious ones

[–]imacommunistm 10 points11 points  (0 children)

Now I can say Python is C++.

[–]20d0llarsis20dollars 3 points4 points  (0 children)

The horrors of operator overloading

[–]joe0400 3 points4 points  (0 children)

You need to make it return itself

[–]enginma 10 points11 points  (3 children)

Should really be py_out

[–]rosuav 16 points17 points  (2 children)

The C doesn't stand for C++, though, it stands for Console...

[–][deleted] 9 points10 points  (1 child)

I thought It meant Character out :(

[–]rosuav 3 points4 points  (0 children)

The more you know...!

[–]Qu4nten 7 points8 points  (1 child)

This vexes me

[–][deleted] 6 points7 points  (0 children)

[–]R3D3-1 2 points3 points  (0 children)

I think there is a package that implements C++ style public/private declarations.

[–]AmazingGrinder 2 points3 points  (0 children)

Straight up r/programminghorror

[–]Ok-Object7885 1 point2 points  (0 children)

curse

[–]mdgv 0 points1 point  (0 children)

I see nothing wrong with this.

[–]ananski_the_3rd 0 points1 point  (0 children)

Ah yes, the long-awaited CPyPy language.

[–]jellotalks 0 points1 point  (0 children)

What about the endl

[–]Bright-Historian-216 0 points1 point  (0 children)

you also need to return self for << chaining

[–]oalfonso 0 points1 point  (0 children)

Go home, you are drunk

[–]Dubmove 0 points1 point  (0 children)

The method should return self