Why is lshift not changing the values of the string

Wouldn’t the lshift dunder change the value of the string

Hi @Anuvrat99

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.

Hope this might helps :slightly_smiling_face:

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.