repr and super in Python 2.7. `super().__repr__` vs `repr(super())` [duplicate]
Asked Answered
J

0

6

Question: Why is repr(super(class, thing)) different from super(class, thing).__repr__()?

I've come across something that I think is odd, would be great if someone has an explanation for why this works the way it does.

My question is about how repr works

class MyInt(int):
    pass

one = MyInt(1)
sup = super(MyInt, one)

>>> repr(sup)

"<super: <class 'MyInt'>, <MyInt object>>"

while

>>> sup.__repr__()

'1'

The second is what I expected to come from the first (yes, the quotes are different in each case). Why are these cases of repr different? I thought repr(x) just called x.__repr__.


As far as I know, super provides an API to the methods of the super class, rather than the super class itself. So it seems to me that my sup has two __repr__ methods, one which is called by the repr and one which is under the name __repr__. Would really appreciate some insight into what's going on here.

Jones answered 22/11, 2017 at 11:3 Comment(2)
To clarify, are you asking why super(MyInt, one).__repr__() and repr(super(MyInt, one)) are different?Angrist
exactly, I thought the second was just calling the firstJones

© 2022 - 2024 — McMap. All rights reserved.