摘要:本文主要向大家介绍了Python语言入门的属性函数(@property),通过具体的实例让大家了解,希望对大家学习Python语言入门有所帮助。
本文主要向大家介绍了Python语言入门的属性函数(@property),通过具体的实例让大家了解,希望对大家学习Python语言入门有所帮助。
属性函数(@property)
在对象中两个很重要的元素就是属性和方法,在调用的时候两者是有区别的。
class People: def __init__(self,first_name,last_name): self.first_name = first_name self.last_name = last_name def get_first_name(self): return self.first_name a = People('lala','ouyang') print(a.get_first_name()) print(a.first_name)
从例子中我们可以发现,一样的结果,但是调用的过程不一样(虽然其实也就是多一个括号而已),那么有没有一种办法,使得我们调用属性的时候就会自动调用相应的方法,也就是增加一些额外的处理过程(例如类型检查或者验证)。这时候属性函数(@property)就能给我们提供很好的解决方案。
首先是最简单的例子,自动调用get,set,del函数对属性的处理。
class People: def __init__(self,name): self.name = name #getter function @property #属性函数 def name(self): return self._name #setter function @name.setter def name(self,name): self._name = name #deleter function @name.deleter def name(self): raise AttributeError('Can not delete the name') a = People('leida') print(a.name) #calling the getter function automatic when we get the attribute a.name = 'libai' #Calling the setter function automatic when we set the attribute print(a.name)del a.name #Calling the deleter function automatic when we delete the attribute leida Traceback (most recent call last): libai File "D:/home/WX/test_clsaa.py", line 65, in <module> del a.name #Calling the deleter function automatic when we delete the attribute File "D:/home/WX/test_clsaa.py", line 59, in name raise AttributeError('Can not delete the name') AttributeError: Can not delete the name
正如例子中这样。要定义对属性的访问,一种最简单的方法就是将其定义为property。比如说,增加对属性的类型检查:
class People: def __init__(self,name): self.name = name #getter function @property #属性函数 def name(self): return self._name #setter function @name.setter def name(self,name): if not isinstance(name,str): raise TypeError('name must is string type') self._name = name #deleter function @name.deleter def name(self): raise AttributeError('Can not delete the name') a = People(12) #calling the setter function automatic when create the instance Traceback (most recent call last): File "D:/home/WX/test_clsaa.py", line 63, in <module> a = People(12) #calling the setter function automatic when create the instance File "D:/home/WX/test_clsaa.py", line 45, in __init__ self.name = name File "D:/home/WX/test_clsaa.py", line 55, in name raise TypeError('name must is string type') TypeError: name must is string type
当我们实现一个property时,底层数据仍然需要保存在某个地方,因而在get和set的方法中,可以看到直接对_name操作的,这就是数据实际保存的地方。但是,也发现在init()方法中任然是对self.name操作的。但是实际情况是我初始化的时候程序仍旧是对self._name操作的。(这点我也还不理解,应该不是这样的啊.但是必须这么写,不然会报错)。
对于已经存在的get,set,del方法,同样也可以定义为property:
class People: def __init__(self,name): self.name = name def get_name(self): print('calling the get function') return self._name def set_name(self,name): print('calling the set function') self._name = name def del_name(self): del self._name name = property(get_name,set_name,del_name) a = People('libai') #calling the setter function automatic when create the instance
同时,在set_name 函数中做了打印标记,发现在init()方法中确实调用了set_name()函数。
Property属性实际上就是把一系列的方法绑定到一起。如果检查类的property属性,就会发现property自身所持有的属性fget,fset,fdel所代表的原始方法。
print(People.name.fget) print(People.name.fset) print(People.name.fdel)
一般来说,我们不会直接去调用fset或者fget,但是当我们调用property属性时会自动触发对这些方法的调用。
上面例子中的两种写法,一般倾向于第二种写法,特别是如果需要对某个普通的属性额外增加处理步骤时,可以在不修改已有代码的情况下将这个属性提升为一个property。
Property也可以用来定义需要计算的属性,这类属性并不会实际被保存起来,而是根据需要完成计算。
import mathclass Circle: def __init__(self,radius): self.radius = radius @property def area(self): return math.pi * self.radius**2 @property def perimeter(self): return 2*math.pi*self.radius c = Circle(5) print(c.area) #do not need (): c.area instead of c.area() print(c.perimeter) 78.5398163397448331.41592653589793
这样的写法就会使得实例的接口变得统一,本来用方法实现的计算调用的时候用属性就可以,很好的避免了方法、属性傻傻分不清的情况了。
个人建议,不要在代码中不断重复使用@property,这样会使得代码变得臃肿,而且难以阅读,容易出错。同样的任务,利用描述符或者闭包也能够很好的完成(以后会详细解释)。
本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号