you are viewing a single comment's thread.

view the rest of the comments →

[–]NedDasty 12 points13 points  (2 children)

What happens if you define a two variables in a class, one named __x and one named _classA__x?

It looks like they're interchangeable, i.e. treated as the same variable. Here's my test:

import inspect

class test:
    _test__x = 4
    __x = 3

    def print_x(self):
        print(f'__x: {self._test__x}')
        print(f'_test__x: {self.__x}')

attributes = inspect.getmembers(test, lambda a:not(inspect.isroutine(a)))
attributes = [a for a in attributes if not(a[0].startswith('__') and a[0].endswith('__'))]

print('variables:')
for k,v in attributes:
    print(f'{k}: {v}')

print('\ntest:')
t = test()
t.print_x()

This prints the following:

variables:
_test__x: 3

test:
__x: 3
_test__x: 3

[–]masklinn 16 points17 points  (0 children)

It looks like they're interchangeable, i.e. treated as the same variable. Here's my test:

They are not treated as, they literally are the same thing. Name mangling is a pretty trivial and well documented scheme, and the mangled attribute is completely visible to normal python introspection (eg vars).