Why are Python's 'private' methods not actually private?

后端 未结 12 2267
不思量自难忘°
不思量自难忘° 2020-11-22 02:50

Python gives us the ability to create \'private\' methods and variables within a class by prepending double underscores to the name, like this: __myPrivateMethod()

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 03:04

    Why are Python's 'private' methods not actually private?

    As I understand it, they can't be private. How could privacy be enforced?

    The obvious answer is "private members can only be accessed through self", but that wouldn't work - self is not special in Python, it is nothing more than a commonly-used name for the first parameter of a function.

提交回复
热议问题