private public protected access specifiers in python

时光毁灭记忆、已成空白 提交于 2019-12-10 10:11:46

问题


Can we simulate private and protected access specifiers in python?

Name Mangling

eg:

__var=10

can simulate private but its viable to be accessed outside easily via the object.

object._className__var

So is there a way we could simulate or does python a solution directly which I am not aware of?


回答1:


Python does not have mandatory access control like some other languages you may be used to. The philosophy of the language is "We are all consenting adults".

By convention, private attributes are prefixed with an underscore, which is a hint to people that they shouldn't be used directly. But it's just that, convention. If you want to sandbox Python, you need to do it in a separate process.

The purpose of the double underscore mangling is to prevent accidental name collisions, not to enforce access control.



来源:https://stackoverflow.com/questions/17246867/private-public-protected-access-specifiers-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!