Un ejemplo vale más que 1000 palabras:
class test(object):
def __init__(self):
self.a = 0
self._b = 0
def change_a(self):
self.a = 1
def change_b(self):
self._b = 1
def display_b(self):
print "_b: ", self._b
como está, el resultado es:
c = test()
c.a
0
#expected.
c.b
AttributeError: 'test' object has no attribute 'b'
# because _b is private
c.display_b()
_b: 0
# methods can display private variables.
ahora asignar algunos valores:
c.a=100
c.b=100
c.a
100
# again, expected
c.b
100
# as expected
c.display_b()
_b: 0
# again, private variable can't be changed directly.
Ahora llamar funcs internos: [? ¿Cuál es el significado de una simple y una doble subrayado antes de un nombre de objeto]
c.change_a()
c.change_b()
c.a
1
# expected
c.b
100
# that c.change_b() function isn't changing the externally created variable c.b
c.display_b()
1
#but it changed the internal variable _b
Posible duplicado de (http://stackoverflow.com/questions/1301346/what-is -the-meaning-of-a-single-and-a-double-underscore-before-an-object-name) – MackM