Hacker News new | ask | show | jobs
by eire1130 3193 days ago
Sure, but I think this is what the author is getting at:

  >>> class A:
  ...     def __a(self,b):
  ...         return b
  ...
  >>> A()
  <__main__.A object at 0x0000024EE5B0EBE0>
  >>> A().__a('b')
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  AttributeError: 'A' object has no attribute '__a'
  >>> a=A()
  >>> dir(a)
  ['_A__a', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
  '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
  '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
  >>> a._A__a("b")
  'b'
In practice, I have never seen this used and just confuses things - and for dev's coming from Java / C++ or some other language it tends to just confuse things.
1 comments

Name mangling was never intended as a way to make variables private (or pseudo-private) but as a way to avoid conflicts when designing for inheritance.
Exactly!