python中的私有公共受保护访问说明符 [英] private public protected access specifiers in python

查看:130
本文介绍了python中的私有公共受保护访问说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在python中模拟私有和受保护的访问说明符吗?

Can we simulate private and protected access specifiers in python?

名称修改

例如:

__var=10

可以模拟私有,但可以通过对象轻松地在外部访问。

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

object._className__var

那么有没有一种方法可以模拟或使用python直接解决我不知道的解决方案?

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

推荐答案

Python没有像您惯用的其他语言那样具有强制性的访问控制。这种语言的哲学是我们都是成年人。

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".

按照惯例,私有属性以下划线为前缀,这向人们暗示他们不应该这样做。不能直接使用。但这只是约定。如果您想对Python进行沙盒化,则需要在一个单独的过程中进行。

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.

这篇关于python中的私有公共受保护访问说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆