I would like to use a name mangled name in Python, to avoid subclasses from accessing the attribute. I know I can just do _classname__attributename
, but according to the documentation, "If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. If the class name consists only of underscores, no transformation is done." I'm particularly worried about the "implementation defined" part. It seems there are also some subtleties. Is there a standard library function to convert a class name plus an attribute name into its name mangled equivalent?
Background
I want to decorate a class's docstring to be able to say whether or not it requires certain optional external dependencies, so that it can be automatically enabled or disabled in the doctest runner (full details at https://github.com/sympy/sympy/pull/1969).
Ideally, to do this, you would add an attribute to the docstring, but Python does not allow adding attributes to strings. So instead, you have to add an attribute to the class. What I need is an attribute that is not inherited by subclasses, since docstrings are not inherited. Otherwise, subclasses will have their docstrings incorrectly decorated. My original solution was to use a descriptor object that disables the attribute for subclasses, but this is a little complicated when something like name mangling already exists that basically already does this, excepting this one subtlety.