मराठी

What do you mean by name mangling? Support your answer with a relevant example.

Advertisements
Advertisements

प्रश्न

What do you mean by name mangling? Support your answer with a relevant example.

कोड लेखन
लघु उत्तर
Advertisements

उत्तर

Name mangling is Python's limited mechanism for hiding class attributes. An attribute beginning with two underscores and not ending with two underscores is internally renamed by prefixing it with _ClassName.

class Test:
    def __init__(self):
        self.__marks = 95  # Name-mangled attribute

    def display(self):
        print(self.__marks)


t1 = Test()
t1.display()
print(t1._Test__marks)  # Access using mangled name
95
95

Here, __marks is internally stored as _Test__marks. Name mangling discourages direct access; it does not provide absolute privacy.

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 3: Classes in Python - EXERCISE [पृष्ठ ६२]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
पाठ 3 Classes in Python
EXERCISE | Q 11. | पृष्ठ ६२
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×