python类的私有属性和公共属性说明
作者:yangjiajia123456 时间:2021-04-07 11:03:59
python类私有属性和公共属性
对于python而言,类的属性的可见度只有两种,public和private。
类的私有属性便是在前面加上“__”标识符,而公共属性则不必。
在类的外面访问私有属性会引发异常。
class Base:
def __init__(self, value):
self.__value = value
b = Base(5)
print(assert b.__value)
Traceback (most recent call last):
File "/Users/yangjiajia/Desktop/project/python/algebra/test.py", line 19, in <module>
print(b.__value)
AttributeError: 'Base' object has no attribute '__value'
属性被私有化,即使继承他的字类也不能访问到。
class Parent:
def __init__(self, value):
self.__value = value
class Child(Parent):
def get_value(self):
return self.__value
child = Child(4)
print(child.get_value())
Traceback (most recent call last):
File "/Users/yangjiajia/Desktop/project/python/algebra/test.py", line 24, in <module>
print(child.get_value())
File "/Users/yangjiajia/Desktop/project/python/algebra/test.py", line 21, in get_value
return self.__value
AttributeError: 'Child' object has no attribute '_Child__value'
为何会这样?因为python会对类的私有属性做些转换,以保证private字段的私密性。当编译器看到Child.get_value方法要访问私有属性时,他会先把__value变换为_Child_value然后再进行访问,但实际上该私有属性是_Parent__value。字类无法访问父类的私有属性,只是因为访问的名称不同。
查询该对象的属性字典便知
class Parent:
def __init__(self, value):
self.__value = value
class Child(Parent):
def name(self):
names = 'yang'
def get_value(self):
return self.__value
child = Child(4)
print(child.__dict__)
{'_Parent__value': 4}
python开发的原则还是要少用私有属性,如果需要保证属性不重复,可以在其前面加上单个下划线。
class Parent:
def __init__(self, value):
self._value = value
class Child(Parent):
def get_value(self):
return self._value
child = Child(4)
assert child._value == 4
python私有属性的定义
在Java中用private对变量或者方法进行限定,则方法仅在整个类内部可见,外部不可见、不可访问。
在python中也有私有属性定义,使用函数名前缀的两个下划线进行定义。
例如 __parameter
接下来看上一个Dog的例子
class Dog(object):
__slots__ = ('__name', 'kind', 'level')
def __init__(self,name,kind,level):#构造函数,定义属性和初始方法
self.__name=name
self.kind=kind
self.level=level#赋值
print(f"This is a {self.kind} dog called {self.__name} with level {self.level}")
def run(self):#定义类中的方法
print(f"{self.__name} is now running!")
def roll_over(self):
print(f"{self.__name} is now rolling over!")
def change_level(self):
self.level+=1#修改类中属性的值
print(f"The level of {self.__name} is now {self.level}")
在另一个文件里面进行调用
from Zoo.Dog import Dog#导包
dog=Dog("Halo","Husty",3)#实例化
dog.run()
dog.roll_over()
dog.change_level()#调用方法
发现当我们输入以下内容时,会报错,非法访问。
print(dog.__name)
在本例中,__name是对name进行私有限定。
来源:https://blog.csdn.net/yangjiajia123456/article/details/80383475
标签:python,私有属性,公共属性
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL百万级数据大分页查询优化的实现
2024-01-13 18:22:44
![](https://img.aspxhome.com/file/2023/5/124515_0s.png)
GO语言中回调函数的使用
2024-04-25 15:05:41
django序列化serializers过程解析
2022-09-26 22:22:36
浅谈在不使用ssr的情况下解决Vue单页面SEO问题(2)
2024-04-26 17:37:39
![](https://img.aspxhome.com/file/2023/5/133175_0s.png)
Python 中的异步 for 循环示例详解
2021-07-09 10:27:30
Pandas中的 transform()结合 groupby()用法示例详解
2023-01-26 10:00:09
![](https://img.aspxhome.com/file/2023/5/85135_0s.png)
Web Design广告字体设计技巧
2010-06-24 21:52:00
使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件及出现问题解决方法
2021-09-17 08:53:15
![](https://img.aspxhome.com/file/2023/3/108013_0s.png)
Python通过for循环理解迭代器和生成器实例详解
2022-08-23 14:34:59
java 使用poi 导入Excel数据到数据库的步骤
2024-01-19 11:21:34
![](https://img.aspxhome.com/file/2023/4/66684_0s.png)
从生成CRD到编写自定义控制器教程示例
2024-05-05 09:30:37
Golang空结构体struct{}用途,你知道吗
2024-04-26 17:36:56
Python版的文曲星猜数字游戏代码
2023-08-12 16:39:19
Python 40行代码实现人脸识别功能
2022-09-30 09:58:35
![](https://img.aspxhome.com/file/2023/9/76859_0s.jpg)
php 图片上添加透明度渐变的效果
2023-10-23 09:00:02
python请求域名requests.(url = 地址)报错
2021-12-18 06:28:36
![](https://img.aspxhome.com/file/2023/0/115830_0s.png)
用python 批量更改图像尺寸到统一大小的方法
2021-04-04 23:47:42
python BeautifulSoup使用方法详解
2021-04-22 12:09:55
不同浏览器所支持的“事件”
2007-09-26 18:29:00
python的多重继承的理解
2021-04-27 16:54:21
![](https://img.aspxhome.com/file/2023/7/101737_0s.jpg)