python基础之类的特性(property)
一 什么是特性property
property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值。
import math
class Circle:
def __init__(self,radius): #圆的半径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(10)
print(c.radius)
print(c.area) #可以向访问数据属性一样去访问area,会触发一个函数的执行,动态计算出一个值
print(c.perimeter) #同上
'''
输出结果:
314.1592653589793
62.83185307179586
'''
注意:此时的特性arear和perimeter不能被赋值
c.area=3 #为特性area赋值
'''
抛出异常:
AttributeError: can't set attribute
'''
二、为什么要用property
将一个类的函数定义成特性以后,对象再去使用的时候obj.name,根本无法察觉自己的name是执行了一个函数然后计算出来的,这种特性的使用方式遵循了统一访问的原则。
1、
class People:
def __init__(self,name,age,height,weight):
self.name=name
self.age=age
self.height=height
self.weight=weight
@property
def bodyindex(self):
return self.weight/(self.height**2) p1=People('cobila',38,1.65,74)
print(p1.bodyindex)
p1.weight=200
print(p1.bodyindex)
2、
#被property装饰的属性会优先于对象的属性被使用
#而被propery装饰的属性,如sex,分成三种:
# 1.property
# 2.sex.setter
# 3.sex.deleter
class People:
def __init__(self,name,SEX):
self.name=name
# self.__sex=SEX
self.sex=SEX#p1.sex='male' @property
def sex(self):
print('------proerty---------------------->')
return self.__sex @sex.setter
def sex(self,value):
print('===================》')
# print(self,value)
# if not isinstance(value,str):
# raise TypeError('性别必须是字符串类型')
self.__sex=value #p1.ABCDEFG='male'
@sex.deleter
def sex(self):
print('delete 操作')
del self.__sex p1=People('egon','male')
print(p1.__dict__)
print(p1.sex)
# del p1.sex
# print(p1.sex)
参考链接:http://www.cnblogs.com/linhaifeng/articles/6182264.html
python基础之类的特性(property)的更多相关文章
- Py修行路 python基础 (十七)property 静态方法 类方法
一.property 属性 特性 (装饰器是可调用对象,被装饰对象也是可调用对象) 1.在类内函数属性上添加一个@property,就会对函数属性进行伪装. import math class C ...
- python基础-函数基本特性和用法
函数: 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因变量,y是x的函数.自变量x的取值 ...
- python基础知识(属性property)
属性property property != 类属性和实例属性 访问计算后所得的值 返回所存储的值 创建用于计算的属性 @proper ...
- python基础--几个特性
1.helloword程序的解释 #!/usr/bin/python3 print("Hello, World!") 关于脚本第一行的 #!/usr/bin/python 的解释, ...
- python基础——特性(property)、静态方法(staticmethod)和类方法(classmethod)
python基础--特性(property) 1 什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 import math class Circl ...
- python 3全栈开发-面向对象之绑定方法(classmethod与staticmethod的区别)、多态、封装的特性property
一.面向对象绑定方法 一.类中定义的函数分成两大类 1.绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入): 1. 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 ...
- Python 基础 面向对象之二 三大特性
Python 基础 面向对象之二 三大特性 上一篇主要介绍了Python中,面向对象的类和对象的定义及实例的简单应用,本篇继续接着上篇来谈,在这一篇中我们重点要谈及的内容有:Python 类的成员.成 ...
- python基础(25):面向对象三大特性二(多态、封装)
1. 多态 1.1 什么是多态 多态指的是一类事物有多种形态. 动物有多种形态:人,狗,猪. import abc class Animal(metaclass=abc.ABCMeta): #同一类事 ...
- python基础——使用@property
python基础——使用@property 在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9 ...
随机推荐
- Jetty - Container源码分析
1. 描述 Container提供管理bean的能力. 基于Jetty-9.4.8.v20171121. 1.1 API public interface Container { // 增加一个bea ...
- 利用C#的指针编写都一个简单链表
using System; namespace UnsafeTest { unsafe struct link { public int x; public link* next; } class P ...
- EM5-PE2B
1. vocabulary once adv. 一次,曾经 They cocktailed once every week. He once lived in shanghai. twice adv ...
- 李洪强和你一起学习前端之(4)HTML5介绍
1.1认识HTML5 html的版本: html4 Xhtml1.0 目前: html5是最高的版本 再怎么变化,无非是多了一些标签而已,但是不单单是提供了一些标签 比如: 开发网页游戏 我们可以开发 ...
- vi全文替换命令
1,$s/str1/str2/g:从第一行到最后一行把str1替换成str2
- linux学习笔记命令篇1---命令ls
前言: linux中接触最多的就是命令和文件. 命令 命令是有其格式的, 一般格式是command [option] parameter1 parameter2 [paramete3 ...]: 注 ...
- root-me web server 10-20 writeup
File upload - double extensions文件上传--双扩展 Gallery v0.02 介绍 Your goal is to hack this photo galery by ...
- extjs经典form的submit()和ajax()
extjs 的submit: // 发送请求 this.formPanel.getForm().submit({ u ...
- 《Netty in action》目录修复版本分享
最近阅读了Netty in action一书.深感外国友人的书籍编写能力强大.作者由简入深.精简描述了Netty的相关知识,如何使用等等. 本来想翻译一下的.尝试着翻译了一点之后.发现非常痛苦啊.ps ...
- Flex远程访问获取数据--HTTPService
编写service类: package services { import com.adobe.serialization.json.JSON; import log.LogUtil; import ...