0

I am trying to understand why the following code provides two different outcomes:

int(5).__add__(str(5))

Returns

NotImplemented

Shouldn't it then this return NotImplemented as well?

a = int(5)
b = str(5)
a+b

TypeError: unsupported operand type(s) for +: 'int' and 'str' While

5 + '5'

Returns

TypeError: unsupported operand type(s) for +: 'int' and 'str'

So it knows that the first 5 is an int type but can't use it on 5.__add__(5)

Does int class gets initialized differently when I assign an int to a variable rather than just using an int right away and treats objects differently when used in parentheses?

Jonas Palačionis
  • 4,591
  • 4
  • 22
  • 55

0 Answers0