python - Brython 中的 super().__str__() 是否应该返回不同的东西?
问题描述
我使用 Brython 和str方法继承遇到了一个奇怪的情况。这是我使用Brython 控制台的测试:
>>> class A(object):
... def __str__(self):
... return "A __str__ output."
...
>>> class B(A):
... def __str__(self):
... return super().__str__() + " (from B)"
...
>>> x = A()
>>> x
<__main__.A object>
>>> y = B()
>>> y
<__main__.B object>
>>> str(y)
"<super: <class 'B'>, <B object>> (from B)"
我期待最后一行返回:
"A __str__ output. (from B)"
我在这里做错了吗?
解决方案
相同的代码在 CPython 中运行良好,因此它可能是 Brython 错误。
Python 3.7.6 (default, Dec 30 2019, 19:38:26)
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
... def __str__(self):
... return "A __str__ output."
...
>>> class B(A):
... def __str__(self):
... return super().__str__() + " (from B)"
...
>>> x = A()
>>> x
<__main__.A object at 0x1048de590>
>>> y = B()
>>> y
<__main__.B object at 0x1048de790>
>>> str(y)
'A __str__ output. (from B)'
>>>
推荐阅读
- linux - Linux 与 Windows 中的系统库
- c# - 根据用户对一系列问题的回答隐藏 Word 文档中的部分
- javascript - 使用用户名获取文档参考
- c - 在 C99 的除法中使用指针,错误:二进制操作数无效
- java - 无法通过 Dockerized WebLogic 将 Debug 设置为适用于 Java,而不是使用 intelliJ 或 Studio Code
- twitter-bootstrap-3 - Bootstrap col-6 col-md-4 在手机上不起作用
- python - FileNotFoundError:[Errno 2] 没有这样的文件或目录:'tinycss2\\VERSION'
- react-native - 使用本机反应的任务:app:validateSigningRelease FAILED(未找到签名配置'release'。)
- java - 交换 LinkedList 中的项目,因此较小的值优先
- sql - 使用 Pandas 从数据库读取时,有没有办法排除表名?