Python中设置变量访问权限的方法
作者:廖雪峰 时间:2023-09-13 15:42:15
在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑。
但是,从前面Student类的定义来看,外部代码还是可以自由地修改一个实例的name、score属性:
>>> bart = Student('Bart Simpson', 98)
>>> bart.score
98
>>> bart.score = 59
>>> bart.score
59
如果要让内部属性不被外部访问,可以把属性的名称前加上两个下划线__,在Python中,实例的变量名如果以__开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问,所以,我们把Student类改一改:
class Student(object):
def __init__(self, name, score):
self.__name = name
self.__score = score
def print_score(self):
print '%s: %s' % (self.__name, self.__score)
改完后,对于外部代码来说,没什么变动,但是已经无法从外部访问实例变量.__name和实例变量.__score了:
>>> bart = Student('Bart Simpson', 98)
>>> bart.__name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Student' object has no attribute '__name'
这样就确保了外部代码不能随意修改对象内部的状态,这样通过访问限制的保护,代码更加健壮。
但是如果外部代码要获取name和score怎么办?可以给Student类增加get_name和get_score这样的方法:
class Student(object):
...
def get_name(self):
return self.__name
def get_score(self):
return self.__score
如果又要允许外部代码修改score怎么办?可以给Student类增加set_score方法:
class Student(object):
...
def set_score(self, score):
self.__score = score
你也许会问,原先那种直接通过bart.score = 59也可以修改啊,为什么要定义一个方法大费周折?因为在方法中,可以对参数做检查,避免传入无效的参数:
class Student(object):
...
def set_score(self, score):
if 0 <= score <= 100:
self.__score = score
else:
raise ValueError('bad score')
需要注意的是,在Python中,变量名类似__xxx__的,也就是以双下划线开头,并且以双下划线结尾的,是特殊变量,特殊变量是可以直接访问的,不是private变量,所以,不能用__name__、__score__这样的变量名。
有些时候,你会看到以一个下划线开头的实例变量名,比如_name,这样的实例变量外部是可以访问的,但是,按照约定俗成的规定,当你看到这样的变量时,意思就是,“虽然我可以被访问,但是,请把我视为私有变量,不要随意访问”。
双下划线开头的实例变量是不是一定不能从外部访问呢?其实也不是。不能直接访问__name是因为Python解释器对外把__name变量改成了_Student__name,所以,仍然可以通过_Student__name来访问__name变量:
>>> bart._Student__name
'Bart Simpson'
但是强烈建议你不要这么干,因为不同版本的Python解释器可能会把__name改成不同的变量名。
总的来说就是,Python本身没有任何机制阻止你干坏事,一切全靠自觉。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中WSGI是什么,Python应用WSGI详解
SQL Server 数据库基本操作语句总结
MySQL复制表结构和内容到另一张表中的SQL语句
在ubuntu18.04版本安装vscode(2种)
![](https://img.aspxhome.com/file/2023/4/123854_0s.jpg)
MySQL 选择合适的存储引擎
![](https://img.aspxhome.com/file/2023/0/70460_0s.png)
Go语言中的闭包详解
Python恋爱小助手之必拿下
![](https://img.aspxhome.com/file/2023/8/120438_0s.jpg)
详解python中读取和查看图片的6种方法
![](https://img.aspxhome.com/file/2023/1/87511_0s.png)
Python matplotlib绘图设置图例案例
![](https://img.aspxhome.com/file/2023/4/110214_0s.png)
php数组查询元素位置的实例方法
Vue子组件监听父组件值的变化
如何添加一个mysql用户并给予权限详解
![](https://img.aspxhome.com/file/2023/4/127564_0s.png)
重学Go语言之运算符与控制结构详解
YUI学习笔记(3)
javascript的this关键字详解
简单三步轻松实现ORACLE字段自增
oracle命令行删除与创建用户
Golang Gob编码(gob包的使用详解)
![](https://img.aspxhome.com/file/2023/2/109722_0s.png)
Python自制一个PDF转PNG图片小工具
![](https://img.aspxhome.com/file/2023/8/115518_0s.png)
将python项目打包成exe与安装包的全过程
![](https://img.aspxhome.com/file/2023/4/114604_0s.jpg)