Creating cout in python

The code is :

class Ostream:
def lshift(self, other):
print(other, end=’ ')
return self
cout=Ostream( )

now input is cout<< “Hello” << “World”

In video it is explained that as return is self(return self) in the code so first of all cout<<“Hello” is executed and then cout<<“World” is left, which is further executed.

I didn’t understood it at all, please explain the concept.

hey @lucky.05 ,

class Ostream:

def __lshift__(self, other):

print(other, end=' ')
return self

cout=Ostream( )

cout << "Hello" << "world"

for first case ,
when we execute cout << “hello” , the lshift function detects variable other value as to be “Hello” ,
then it gets printed and return self ,means it returned hat class object itself , means actually the object which was just cout is now re intialized after printing “hello”.
so it becomes as cout << "world" in the compiler while hello in already printed in output.
Then the same thing happens again and you see a final output as Hello World.

I hope this would have helped you understand it properly.
Thank You and Happy Learning :slightly_smiling_face:.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.