0
votes

name mangling works most of the time but not when a subclass has the same name as a super class; for example if two classes in different modules m2.A extends m1.A, then m2.A.foo will hide m1.A.foo because both of them become _A__foo; is there an option to enable a "fuller" name mangling, namely, mangle class fields with a unique class identifier?

# m1.py
class C:
    def __foo(self):
        print('C1')

    def bar(self):
        self.__foo()

# main.py
from m1 import C as C1

class C(C1):
    # i mangle this for internal use within this class, not expecting it to
    # break a super class method; so i cant safely use a name even if it is
    # mangled; sad, is there a way to mangle with a guid across all classes?
    def __foo(self):
        print('CM')

C().bar()   # CM not C1
2
No. And honestly, you should just stick to having different names for subclasses. I've never seen this sort of thing before and it's a highly unusual use-case - juanpa.arrivillaga
@juanpa.arrivillaga it is hard to guarantee your subclass name is different from any superclass, which could lie in a deeply chained library... - Cyker

2 Answers

1
votes

The module name is designed to be a part of the namespace, I think the simplest answer of 'full name mangling' is just add the module name.

0
votes

If you can't come up with a meaningful name in the subclass that is different from the superclass, that should be telling you one of 2 things:

  • Something is not quite right with the design. Is your subclass really so similar to the superclass that you must name it the same (or: is the attribute in the subclass really that similar to something in the superclass)? If that is the case, does it even make sense to make a subclass (or to add it only in the subclass?)?

  • Your names are not meaningful. Give things more descriptive names and this problem will go away.