Update 09.面向对象进阶.md

pull/854/head
wwwgood2012 2022-01-13 18:22:36 +08:00 committed by GitHub
parent a259a6abd3
commit cc3c15fbb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 0 deletions

View File

@ -47,6 +47,62 @@ if __name__ == '__main__':
main()
```
```python
python @property的介绍与使用
python的@property是python的一种装饰器是用来修饰方法的。
作用:
我们可以使用@property装饰器来创建只读属性@property装饰器会将方法转换为相同名称的只读属性,可以与所定义的属性配合使用,这样可以防止属性被修改。
使用场景:
1.修饰方法,是方法可以像属性一样访问。
class DataSet(object):
@property
def method_with_property(self): ##含有@property
return 15
def method_without_property(self): ##不含@property
return 15
l = DataSet()
print(l.method_with_property) # 加了@property后可以用调用属性的形式来调用方法,后面不需要加()。
print(l.method_without_property()) #没有加@property , 必须使用正常的调用方法的形式,即在后面加()
两个都输出为15。
class DataSet(object):
@property
def method_with_property(self): ##含有@property
return 15
l = DataSet()
print(l.method_with_property) # 加了@property后可以用调用属性的形式来调用方法,后面不需要加()。
如果使用property进行修饰后又在调用的时候方法后面添加了() 那么就会显示错误信息TypeError: 'int' object is not callable也就是说添加@property 后,这个方法就变成了一个属性,如果后面加入了()那么就是当作函数来调用而它却不是callable可调用的。
class DataSet(object):
def method_without_property(self): ##不含@property
return 15
l = DataSet()
print(l.method_without_property #没有加@property , 必须使用正常的调用方法的形式,即在后面加()
没有使用property修饰它是一种方法如果把括号去掉不会报错输出的就会是方法存放的地址。
2.与所定义的属性配合使用,这样可以防止属性被修改。
由于python进行属性的定义时没办法设置私有属性因此要通过@property的方法来进行设置。这样可以隐藏属性名让用户进行使用的时候无法随意修改。
class DataSet(object):
def __init__(self):
self._images = 1
self._labels = 2 #定义属性的名称
@property
def images(self): #方法加入@property后这个方法相当于一个属性这个属性可以让用户进行使用而且用户有没办法随意修改。
return self._images
@property
def labels(self):
return self._labels
l = DataSet()
#用户进行属性调用的时候直接调用images即可而不用知道属性名_images因此用户无法更改属性从而保护了类的属性。
print(l.images) # 加了@property后可以用调用属性的形式来调用方法,后面不需要加()。
```
### \_\_slots\_\_魔法
我们讲到这里不知道大家是否已经意识到Python是一门[动态语言](https://zh.wikipedia.org/wiki/%E5%8A%A8%E6%80%81%E8%AF%AD%E8%A8%80)。通常,动态语言允许我们在程序运行时给对象绑定新的属性或方法,当然也可以对已经绑定的属性和方法进行解绑定。但是如果我们需要限定自定义类型的对象只能绑定某些属性,可以通过在类中定义\_\_slots\_\_变量来进行限定。需要注意的是\_\_slots\_\_的限定只对当前类的对象生效对子类并不起任何作用。