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

后端 未结 12 2135
不思量自难忘°
不思量自难忘° 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:16

    It's just one of those language design choices. On some level they are justified. They make it so you need to go pretty far out of your way to try and call the method, and if you really need it that badly, you must have a pretty good reason!

    Debugging hooks and testing come to mind as possible applications, used responsibly of course.

提交回复
热议问题