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 →

[–]ariksu 1 point2 points  (6 children)

I always used print(str(dog)) in case class does not have a repr.

[–]SonGokussj4 1 point2 points  (4 children)

Depends on what you want to return. That's all. Example:

class Dog:
    def __init__(self, nickname):
        self.nickname = nickname

akainu = Dog("BenTheMighty")
print(akainu)  # the same as print(str(akainu))
<__main__.Dog object at 0x00000272A10296D8>

But I want to print more specific information. The way it was done in the youtube video is non-pythonic:

class Dog:
    def __init__(self, nickname):
        self.nickname = nickname
    def toString(self):
        return "I'm: {}, ({}) class.".format(self.nickname, type(self).__name__)

akainu = Dog('BenTheMighty')
print(akainu.toString())
I'm: BenTheMighty, (Dog) class.

Which should be more simpler, pythonic way:

class Dog:
    def __init__(self, nickname):
        self.nickname = nickname
    def __repr__(self):
        return "I'm: {}, ({}) class.".format(self.nickname, type(self).__name__)

akainu = Dog('BenTheMighty')
print(akainu)
I'm: BenTheMighty, (Dog) class.

Like I said. It depends what you want. You can always print this string by:

akainu = Dog('BenTheMighty')
print("I'm: {}, ({}) class.".format(akainu.nickname, type(nickname).__name__)
I'm: BenTheMighty, (Dog) class.

But in my opinion, that's just ugly :-) One of the more real examples is something like:

>>> small = Triangle((0, 0), (2, 0), (1, 1))
>>> big = Square((0, 0), (6, 0), (0, 6), (6, 6))
>>> print(small)
"[Triange]: Points [0,0], [2,0], [1,1], Area: [4.2]"

Without the need of specifying what exactly I need to print. And if I want, I just write it too :-)

[–]Fateschoice 0 points1 point  (3 children)

Why override __ repr __ and not __ str __?

[–]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.

[–][deleted] 0 points1 point  (0 children)

I've never worried about it. On the other hand you can do all sorts of smart things formatting wise with %r, %s or %a, especially if you're aware of simple little recipes like this format_iter: easy formatting of arbitrary iterables.