Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name mangling in python

Consider the following program.

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

 ^^^^^^^

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

like image 936
vks Avatar asked Dec 21 '25 03:12

vks


1 Answers

It's right there in the docs (emphasis added):

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

like image 97
BrenBarn Avatar answered Dec 23 '25 17:12

BrenBarn