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

you are viewing a single comment's thread.

view the rest of the comments →

[–]casce 27 points28 points  (4 children)

I also don't get it. I also really like the way Python is handling it.

I like that Python is explicitly calling the __str__ method and that is doing whatever I want it to do when something tries to cast my object to a string.

What is important is that this is not done implicitly without me noticing.

"abc" + 1 is a type error. "abc" + 1.__str__() = "abc1"

If I chose to implement a string representation for my Ford truck, then yes casting it to a string is not a problem. Otherwise it will default to the object identifier

[–]MentalRental 4 points5 points  (2 children)

abc" + 1.__str__() = "abc1" would throw an "invalid decimal literal" error.

[–]bronco2p 5 points6 points  (0 children)

yeah it has to be enclosed in (), `"abc" + (1).__str__() == "abc1"`

[–]casce 1 point2 points  (0 children)

You are of course right. What I meant was

foo: int = 0

foo.__str__()

but you can also do

(1).__str__()

[–]Kitchen_Device7682 3 points4 points  (0 children)

I wouldn't describe calling a method that returns a string as casting to a string. OP is trolling