confused about __str__ vs. __repr__

N

Neal Becker

Reading some FAQ, I see that __str__ is "meant for human eyes".

But it seems that:
class X(object):
def __str__(self):
return "str"
def __repr__(self):
return "repr"

x = X()
d = {0 : x}
print d
{0: repr}

So if __str__ is "meant for human eyes", then why isn't print using it!
 
D

Diez B. Roggisch

Neal said:
Reading some FAQ, I see that __str__ is "meant for human eyes".

But it seems that:
class X(object):
    def __str__(self):
        return "str"
    def __repr__(self):
        return "repr"

x = X()
d = {0 : x}
print d
{0: repr}

So if __str__ is "meant for human eyes", then why isn't print using it!

Because the __str__ of dict uses __repr__. Do a

print x

to see that print itself does use __str__.

BTW, could you configure your newsreader to not put the GMANE-group as
follow-up? NNTP doesn't work for that.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top