Python name mangling

后端 未结 11 2346
天命终不由人
天命终不由人 2020-11-22 05:31

In other languages, a general guideline that helps produce better code is always make everything as hidden as possible. If in doubt about whether a variable should be privat

11条回答
  •  清歌不尽
    2020-11-22 06:09

    At first glance it should be the same as for other languages (under "other" I mean Java or C++), but it isn't.

    In Java you made private all variables that shouldn't be accessible outside. In the same time in Python you can't achieve this since there is no "privateness" (as one of Python principles says - "We're all adults"). So double underscore means only "Guys, do not use this field directly". The same meaning has singe underscore, which in the same time doesn't cause any headache when you have to inherit from considered class (just an example of possible problem caused by double underscore).

    So, I'd recommend you to use single underscore by default for "private" members.

提交回复
热议问题