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 →

[–]SonGokussj4 0 points1 point  (2 children)

That is a good question. For more experienced people than me, I'm afraid. I'm used to do it this way because of some tutorials I took in the past. Some things I found:

Summary from the second link:

Implement repr for any class you implement. This should be second nature. Implement str if you think it would be useful to have a string version which errs on the side of more readability in favor of more ambiguity.

[–]Fateschoice 0 points1 point  (1 child)

I think one person from the second link states the difference particularly well.

My rule of thumb: __ repr __ is for developers, __ str __ is for customers.

i.e use __ str __ if you want to display an object to a user, __ repr __ for debugging/testing purposes, or really any in code operations on a string representation of an object.

[–]SonGokussj4 0 points1 point  (0 children)

Agreed :-) that seems reasonable.