python / python/peps

PEP 526: Name mangling doesn't take place inside the body of a module

未关闭
#3,062 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
reStructuredText
星标
5k
派生
1.8k
平均合并
2 天 4 小时
30 天内合并 PR
25

描述

Hi,
In PEP 526, there is a section called "Runtime Effects of Type Annotations" which has the following code:

from typing import Dict
class Player:
    ...
players: Dict[str, Player]
__points: int

print(__annotations__)
# prints: {'players': typing.Dict[str, __main__.Player],
#          '_Player__points': <class 'int'>}

The code and the output don't match.
Name mangling only happens inside the body of the class not module. So the correct output would be:

{'players': typing.Dict[str, __main__.Player], '__points': <class 'int'>}

If we want to see the effect of name mangling we could have something like:

from typing import Dict

class Player:
    __points: int

players: Dict[str, Player]
print(__annotations__)
print(Player.__annotations__)

Or have the print(__annotations__) inside the class:

from typing import Dict

class Player:
    __points: int
    print(__annotations__)

players: Dict[str, Player]
print(__annotations__)

That would be a quick fix if you confirm.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

打开 PEP 526 并查看“Runtime Effects of Type Annotations”部分,尤其是其中的模块级注解示例及显示的输出。根据该示例确认 name-mangling 的行为,然后更新代码或输出,使两者保持一致,并保留对类级 mangling 的清晰演示。完成的标准是,渲染后的 PEP 不再呈现相互矛盾的结果。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
1/5
预计耗时
1 小时以内
活跃度
停滞
描述清晰度
描述清楚
新手友好度
55/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。