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 ...
随机推荐
- 点滴积累【JS】---JS小功能(操作Table--动态添加删除表格及数据)
效果: 代码: <head runat="server"> <title></title> <style type="text/ ...
- Oracle之sqlplus显示中文出现乱码
解决办法: win+R快捷键>>--regedit>>HKEY_LOCL_MACHINE>>SOFTWARE>>ORALCE>>KEY_OR ...
- ERROR C2676
直接上代码: nl.h #ifndef NL_H #define NL_H #include <iosfwd> namespace ZJ { /** Insert a newline ch ...
- HTML5坦克大战(2)绘制坦克复习
html代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...
- powerDesigner建表时选择不同数据库类型
l 使用powerDesigner创建表模型的时候可选择数据库类型 如图设置为mysql:
- Chapter 2 JavaScript Basic
Has 5 primitive types: Undefined, Null, Boolean, String, Number. typeof operator Undefined return u ...
- Angular js ie 7,8 兼容性
Angularjs 官网有云: 1)在html 里面 ,有ng-app 的标签里需要定义个id ,id='ng-app'; 2)ie 7及以下版本需要json2.js或json3.js,主要用来解析 ...
- 第一百五十四节,封装库--JavaScript,表单验证--提交验证
封装库--JavaScript,表单验证--提交验证 将表单的所有必填项,做一个判断函数,填写正确时返回布尔值 最后在提交时,判断每一项是否正确,全部正确才可以 提交 html <div id= ...
- List、Map、Set三个接口存储元素时各有什么特点?
List.Map.Set三个接口存储元素时各有什么特点? 解答: 1)List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置.用户能够使用索引(元素在List中的位置,类似于 ...
- [MachineLearning]KNN
# -*- coding: utf-8 -*- """ Created on Wed Jun 18 11:46:15 2014 @author: hp "&qu ...