简体   繁体   English

有人可以说明python名称修改的过程吗?

[英]Can someone illustrate the procedure of python name mangling?

The doc says 医生

Private name mangling: When an identifier that textually occurs in a class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class. 专用名称修改:当在类定义中以文本方式出现的标识符以两个或多个下划线字符开头且不以两个或多个下划线结尾时,它将被视为该类的私有名称。 Private names are transformed to a longer form before code is generated for them. 在为其生成代码之前,将私有名称转换为更长的形式。

I am trying to get a better understanding about this procedure of Python code to be generated. 我试图更好地理解要生成的Python代码的这个过程。

generated by what? 什么产生的? the Python interpreter? Python解释器?

generated to what? 生成什么? the byte code? 字节码?

is this figure apply this procedure? 这个数字适用于这个程序吗?

在此输入图像描述

can someone illustrate this procedure in more detail? 有人可以更详细地说明这个程序吗?

You've made me curious, so you've made me look this up. 你让我很好奇,所以你让我看起来很好看。 First, when you look at the resulting byte code, it already uses STORE_NAME with the "mangled" name. 首先,当您查看生成的字节代码时,它已经使用带有“损坏”名称的STORE_NAME

Who does it and when then? 谁做到了呢? https://github.com/python/cpython/blob/master/Python/compile.c Holds the answer: https://github.com/python/cpython/blob/master/Python/compile.c保留答案:

This file compiles an abstract syntax tree (AST) into Python bytecode. 此文件将抽象语法树(AST)编译为Python字节码。

And the corresponding function would be _Py_Mangle ( "Name mangling: __private becomes _classname__private ." ) 相应的函数将是_Py_Mangle“名称重整: __private _classname__private变为_classname__private 。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM