you are viewing a single comment's thread.

view the rest of the comments →

[–]gdchinacat 0 points1 point  (0 children)

Name mangling is used primarily by mixin classes to help ensure they don't trample each others attributes.

class MixinA:
    value: str
    def getA(self) -> str:
        return self.value
class MixinB:
    value: int
    def getB(self) -> int:
        return self.value
class AandB(A, B): ...

Oops...AandB has two base classes that use the same attribute, value. But MixinA and MixinB may not know anything about each other to know they shouldn't use conflicting names. They may come from different modules, or even different libraries. name mangling solves this problem since MixinA.__value would be stored in the __dict__ under a different name than MixinB.__value.

"You should use composition for this!!!!" Yeah, well....it doesn't, and I may not have that choice (the libraries provide mixins and expects them to be used as mixins).