2

When using the Python interpreter, just writing

3+4

causes it to print '7'

But when importing some class ('Something', which has its own str and add implemented) and do this:

a=Something(var2)
b=Something(var2)
a+b

the interpreter just prints a message reporting the action (EDIT: by this, I meant the message class.Something object at 0xspam..., which is the output of repr when it's not actually implemented, when I thought I'd get something like the output of str), it doesn't prints the str version of the new object that was just created

Why does this happen? How can it be changed?

EDIT: The problem had to do with confusion over what str and repr do (I didn't know about that repr existed when I asked the question), I thought it was enough for a class to have str implemented to both being able to obtain a string from an object, and being able to print it directly to the interpreter without having to write 'print'. But the latter is something that's actually done by implementing repr appropiately

Jumping cat
  • 77
  • 1
  • 1
  • 5
  • What is "a message reporting the action"? Normally, the interpreter doesn't print any such thing. It only prints prompts, the repr of your results, and the traceback of your exceptions. – abarnert Nov 07 '14 at 01:48
  • by "a message reporting the action" I meant the results of repr, but I didn't know about repr until now, which is what answered my initial question. Thanks to everyone. – Jumping cat Nov 07 '14 at 01:57
  • I'm not sure this is a dup, but you should definitely read [Difference between str and repr](http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python/2626364#2626364), the canonical question on `repr`. – abarnert Nov 07 '14 at 02:00

3 Answers3

2

Here are the rules the interpreter uses:

For anything but an expression statement, nothing is printed.

For an expression statement, the result is assigned to _, then the equivalent of this code happens:

if _ is not None:
    try:
        print(repr(_))
    except Exception as e:
        print(<some message with e and object.__repr__(_)>)

That's really all there is to it.

So, if nothing at all is getting printed, the most likely possibility is that a+b returns None here. For example, that Something.__add__ that you forgot to show us could look like this:

def __add__(self, other):
    tmp = copy.copy(self)
    tmp += other
    # oops, no return tmp

Or maybe a+b returns an object whose repr is the empty string, or all whitespace, and you didn't notice that it printed a blank line.

If something is getting printed, but it's not what you expected, then probably the __repr__ isn't defined the way you expected. Since you only mention defining __str__, you may not realize that __repr__ is a separate function, and if you just inherit the default, it tends to look like <__main__.Spam at 0x115f018d0>.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • @Jumpingcat: In that case, I think John1024's answer may be more of a direct solution. But please, edit your question so we don't have to guess exactly what you're asking. – abarnert Nov 07 '14 at 01:57
  • Sorry, I'll edit it then, I wasn't quite sure about how to word my question. – Jumping cat Nov 07 '14 at 01:59
  • @Jumpingcat: No, don't edit it to say "solved", edit it to answer the questions you were asked. Show us exactly what you typed and what output you got, instead of trying to describe it. – abarnert Nov 07 '14 at 02:00
  • sorry again! I described it now, I hope it's ok. – Jumping cat Nov 07 '14 at 02:10
  • 2
    @Jumpingcat Stackoverflow has customs that are a bit different from elsewhere. If you want to understand better, it may help to read: ["What should I do when someone answers my question?"](http://stackoverflow.com/help/someone-answers) – John1024 Nov 07 '14 at 02:15
1

Here is a class with its own add implemented:

class A(object):

    def __init__(self, x):
        self.x = x

    def __add__(self, other):
        return self.__class__(self.x + other.x)

    def __repr__(self):
        return "%s with x=%s" % (self.__class__, self.x)

Now, let's create some instances and add them:

>>> a = A(3); b = A(2)
>>> a + b
<class 't.A'> with x=5

Yes, it prints.

Without the custom __repr__ as defined above, what prints would be the usual default representation, such as:

<t.A object at 0x7f7d7bc9f990>
John1024
  • 109,961
  • 14
  • 137
  • 171
0

print a+b instead of just a+b, of course

As to why a+b doesn't cause str() to be called on objects, I don't know.

smci
  • 32,567
  • 20
  • 113
  • 146