3

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

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Do u know If there is any particular reason why python does it or its just the language syntax set that way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.