调用 str 时未调用魔术方法 __str__

Magic method __str__ is not called on str call

我是 module-wrapper library and one of the authors of aioify 库的创建者。问题出现在 module-wrapper 库中的魔术方法包装中(我从 aioify.aioify 调用 module_wrapper.wrap,但这并不重要)。

我有以下代码:

#!/usr/bin/env python
from aioify import aioify


async def main():
    from pathlib import Path
    # noinspection PyPep8Naming
    AioPath = aioify(obj=Path)
    return await AioPath.create('/tmp')


if __name__ == '__main__':
    import asyncio
    loop = asyncio.get_event_loop()
    path = loop.run_until_complete(main())
    path_str = str(path)
    print(path_str)

我希望得到以下输出:

/tmp

但我明白了:

<module_wrapper.wrap.<locals>.ObjectProxy object at 0x7f64266a9b00>

我不明白为什么。当我打电话时:

path_str = path.__str__()
print(path_str)

我得到了我所期望的:

/tmp

当我设置断点时 inside 魔术方法包装函数调试器不会停止。

UPD0: 要重现此内容,您需要安装 module-wrapperaioify(从压缩包中安装,因为它未在 PyPI 上发布)。不要忘记之前创建virtualenv!):

pip install module-wrapper==0.1.26
pip install https://github.com/yifeikong/aioify/archive/0.3.0.zip

终于解决了。 mseifert 是对的,问题是我没有包装 class 的魔术方法。请参阅 GitHub 和 PyPI 上的固定版本。