0

What is the reason of following behavior of python:

p=89
x=89
print(p.__repr__,x.__repr__)

ans=<method-wrapper '__repr__' of int object at 0x00000000623E78E0> <method-wrapper '__repr__' of int object at 0x00000000623E78E0>

Here repr returns same location. I have read online that python tags values (unlike C and Java it does not assign value to variable but assign variable to value.)

But it gives different location for following code:

p=898
x=898
print(p.__repr__,x.__repr__)

here it gives result as

<method-wrapper '__repr__' of int object at 0x0000000005E8D370> <method-wrapper '__repr__' of int object at 0x0000000005E8D530>

What is the reason for this strange behaviour?

  • 2
    Firstly, `__repr__` is a method which you should actually call: `p.__repr__()`. Secondly, you shouldn't actually call that, you should call the built-in function: `repr(p)`. Thirdly, if you want the ID, you should call the `id()` function, not `repr()`. Fourthly, no, Python doesn't "tag" values. – Daniel Roseman Mar 19 '18 at 10:46
  • mehtod repr() returns value of p in python3.6. so x.__repr__ is what I wants to use. id also represents same behaviour. – Prashant Singh Mar 19 '18 at 11:04
  • No it doesn't, and no it isn't. All you're doing is printing the string representation of a method - hence `method-wrapper` - which happens to include (in its own `__repr__`, but that's irrelevant) the ID of the object it is called on. And the linked question explains exactly why the IDs work that way. – Daniel Roseman Mar 19 '18 at 11:07
  • Yes linked question answers my query. Thank you – Prashant Singh Mar 19 '18 at 11:08

0 Answers0