views:

485

answers:

3

Python provides private name mangling for class methods and attributes.

Are there any concrete cases where this feature is required, or is it just a carry over from Java and C++?

Please describe a use case where Python name mangling should be used, if any?

Also, I'm not interested in the case where the author is merely trying to prevent accidental external attribute access. I believe this use case is not aligned with the Python programming model.

A: 

The name mangling is there to prevent accidental external attribute access. Mostly, it's there to make sure that there are no name clashes.

Lennart Regebro
I mentioned this in the original question. Are you saying you disagree?
Casey
I'm saying that's the reason. So you don't accidentally have name clashes when you subclass.
Lennart Regebro
+8  A: 

It's partly to prevent accidental internal attribute access. Here's an example:

In your code, which is a library:

class YourClass:
    def __init__(self):
        self.__thing = 1           # Your private member, not part of your API

In my code, in which I'm inheriting from your library class:

class MyClass(YourClass):
    def __init__(self):
        # ...
        self.__thing = "My thing"  # My private member; the name is a coincidence

Without private name mangling, my accidental reuse of your name would break your library.

RichieHindle
IMO, YourClass should define _thing, instead of __thing. This would allow MyClass to access it. Second, I see the benefit of using '__thing' in MyClass, if there is concern that the YourClass implementation will change. The only problem, is that if someone wants to subclass MyClass, then you've made it more difficult for them to access 'MyClass.__thing'. It feels like you are sacrificing future reuse, to protect against an "imaginary" problem. No?
Casey
@Casey: You're right that there are member variables that you want to be visible to subclasses, and those you name with a single underscore. You'd make them `protected` in C++. But there are also members that you *don't* want to be visible to subclasses, because they're part of the private implementation of your class (and as you say, they're subject to the implementation changing). It's these that name mangling is for. This isn't an imaginary problem; I've actually done exactly the thing I'm talking about, with a library that used a single underscore rather than a double for a private member.
RichieHindle
@RichieHindle: Exactly. Except that this is an EXTERNAL access. ;) The subclass is seen as external here.
Lennart Regebro
Aren't the "private" implementation details arbitrary? If I was creating a base class to distribute to the world, and I can't conceive of all the possible usage scenarios, then how can I reliable choose which attributes are "protected" and which are "private". In the end, there will always be a reason that someone might need to access a variable to extend/enhance the class. Using mangled attribute names, then is no benefit, and will simply force the next person to un-mangle them.
Casey
I think we'll have to agree to differ on that. IMHO, a well written library class has two APIs, one for direct users of that class, and one for classes that inherit from it. By convention, the former uses names with no underscores, and the latter can access single-underscore names. But the class still has the right to hide some of its implementation details - those that don't form part of either API - away from both types of user. Otherwise changes to the internal workings of the class become near-impossible, because you can't know anything about how your members are being used by subclasses.
RichieHindle
+7  A: 

From PEP 8:

If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores. This invokes Python's name mangling algorithm, where the name of the class is mangled into the attribute name. This helps avoid attribute name collisions should subclasses inadvertently contain attributes with the same name.

(Emphasis added)

too much php