use*_*182 6 python internals repr super python-2.7
repr(super(class, thing))不同super(class, thing).__repr__()?我遇到过一些我认为很奇怪的事情,如果有人解释为什么它的工作方式会很好,那就太棒了.
我的问题是如何repr运作
class MyInt(int):
pass
one = MyInt(1)
sup = super(MyInt, one)
Run Code Online (Sandbox Code Playgroud)
>>> repr(sup)
"<super: <class 'MyInt'>, <MyInt object>>"
而
>>> sup.__repr__()
'1'
第二个是我期望从第一个开始的(是的,引用在每种情况下都不同).为什么这些案件有所repr不同?我以为repr(x)刚刚打过电话x.__repr__.
据我所知,super为超类的方法提供了一个API,而不是超类本身.所以在我看来,我sup有两个__repr__方法,一个由名称调用repr,一个名称在名称之下__repr__.非常感谢能够深入了解这里发生的事情.
| 归档时间: |
|
| 查看次数: |
217 次 |
| 最近记录: |