python - Brython 中的 super().__str__() 应该返回不同的东西吗?

标签 python brython

我在使用 Brython 和 str 方法继承时遇到了一个奇怪的情况。这是我使用 Brython console 进行的测试:

>>> 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)'
>>>

关于python - Brython 中的 super().__str__() 应该返回不同的东西吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61388716/

相关文章:

python - 连接到 DataFrame 将给出两个单独的起始索引

python - Brython:每 [...] 微秒移动元素

javascript - Brython 将点击事件绑定(bind)到页面中尚未存在的 id

Python/Brython 类型错误 : Cannot use cached_property instance without calling __set_name__ on it

javascript - 集成 Python 和 JavaScript 的最佳方式?

python - 如何使用 Beautiful Soup 将 HTML 5 音频标签添加到 HTML 文档

python - (Python) 如何测试文件中是否包含字符串

python - 为什么我不能在 python 中创建一个轮子?

javascript - 如何将文本输入绑定(bind)到同一 html 页面上的另一个 div

java - python中的正则表达式不匹配单词