InterviewSolution
| 1. |
What will be the output of the below python program? |
|
Answer» class MYCLASS: def __init__(self): self.x=10 self.__y=20 obj=myClass()print(obj.__y) 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. The transformation inserts the class name in FRONT of the name, with leading underscores removed, and a single underscore inserted in front of the class name. For example, the identifier __spam occurring in a class named Ham will be transformed to _Ham__spam. Hence the above example throws an error 'AttributeError: myClass object has no attribute __y' because the name is transformed into _myClass__y. So if we instead print(obj._myClass__y), this will work and prints 20. |
|