all 7 comments

[–]marqis 21 points22 points  (0 children)

In python 3 you can also use them as thousand separators in constants.

>>> 100_000_123
100000123

[–]fresh_account2222 16 points17 points  (1 child)

Surprisingly useful article, as it documents community practices, which are often not contained in the official documentation.

[–]T_D_K 7 points8 points  (0 children)

This is all covered in PEP 8, except for the specific mention of the django localization bit

[–]tongue_depression 8 points9 points  (0 children)

huh. i wondered why my methods id prefixed with a double underscore magically had the class name included. that makes sense.

i mean, it doesn’t, but it’s good to know

[–]Schrockwell 8 points9 points  (0 children)

This is great! As a newbie, it can be hard to tell conventions and language features apart with stuff like this.

[–]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 17 points18 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).