python中的名称修改 [英] Name mangling in python

查看:75
本文介绍了python中的名称修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下程序.

 class A():
     class __B():
         def __c(self):
             pass
a = A()
dir(a)
['_A__B', '__doc__', '__module__']
dir(a._A__B)
['_B__c', '__doc__', '__module__']

 ^^^^^^^

为什么不是___B__c 为什么没有3 _s.这里的逻辑是什么?根据name mangling,应该是_classclass这里是__B.那么2 在哪里>__s

Why is this not ___B__c why is not there not 3 _s.What is the logic here? According to name mangling,it should be _class and class here is __B.So where are the 2 __s

推荐答案

就在 文档(强调):

任何 __spam 形式的标识符(至少两个前导下划线,最多一个尾随下划线)都被文本替换为 _classname__spam,其中 classname 是当前类名去掉了前导下划线

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped

这篇关于python中的名称修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆