property 是一个内置的装饰器函数,只在面向对象中使用

求一个圆的周长和面积

# 周长和面积都是通过类的方法得到
from math import pi
class Cricle:
def __init__(self,r):
self.r = r def getPerimeter(self):
return 2*pi*self.r def getArea(self):
return pi*self.r**2
c1 = Cricle(5)
print(c1.getPerimeter())
print(c1.getArea())
# 人的BMI,它是计算得来得,但更像是一个人的属性

# 传统做法
class Person:
def __init__(self,name,high,weight):
self.name = name
self.high = high
self.weight = weight def getBIM(self):
return self.weight/(self.high**2) wangys=Person('wangys',1.75,70)
print(wangys.getBIM()) # perporty
class Person:
def __init__(self,name,high,weight):
self.name = name
self.high = high
self.weight = weight @property
def bim(self):
return self.weight/(self.high**2) wangys=Person('wangys',1.75,70)
print(wangys.bim)
class Goods: # 定义了一个商品类
discount = 0.5 # 折扣是5折
def __init__(self,name,price):
self.name = name
self.__price = price # 将需要修改的属性设置成为私有属性 @property
def price(self): # 定义一个正常的属性,然后做相应的操作
return self.__price * Goods.discount apple = Goods('apple',10)
print(apple.price)
class Persion:
def __init__(self,name):
self.__name = name @property
def name(self):
return self.__name
@name.setter
def name(self,new_name):
self.__name = new_name
# 但是圆的周长和面积更像圆的属性,使用property 将一个函数(无参数)伪装成一个类的属性

from math import  pi
class Cricle:
def __init__(self,r):
self.r = r @property
def perimeter(self):
return 2*pi*self.r
@property
def area(self):
return pi*self.r**2 c1 = Cricle(5)
print(c1.perimeter)
print(c1.area)

Python内置函数之-property的更多相关文章

  1. Python内置函数(63)——property

    英文文档: class property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget is ...

  2. Python内置函数(51)——property

    英文文档: class property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget is ...

  3. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  4. python内置函数大全(分类)

    python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...

  5. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

  6. Python内置函数7

    Python内置函数7 1.propertypython内置的一个装饰器可参考https://blog.csdn.net/u013205877/article/details/77804137 2.q ...

  7. Python内置函数6

    Python内置函数6 1.license() 输出当前python 的license信息 A. HISTORY OF THE SOFTWARE ========================== ...

  8. Python补充--Python内置函数清单

    Python内置函数 Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print(&quo ...

  9. python之Python内置函数一览表

    Python 解释器自带的函数叫做内置函数,这些函数可以直接使用,不需要导入某个模块. 如果你熟悉 Shell 编程,了解什么是 Shell 内置命令,那么你也很容易理解什么是 Python 内置函数 ...

随机推荐

  1. Shell命令-文件压缩解压缩之tar、unzip

    文件及内容处理 - tar.unip 1.tar:打包压缩命令 tar命令的功能说明 tar 命令常用语用于备份文件,tar 是用来建立,还原备份文件的工具程序,它可以加入,解开备份文件内的文件 ta ...

  2. Spring Boot的web开发

    web开发的自动配置类:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration 自动配置的ViewResolver 视图的 ...

  3. Transaction check error: file /etc/rpm/macros.ghc-srpm from install of redhat-rpm-config-9.1.0-80.el7.centos.noarch conflicts with file from package epel-release-6-8.noarch Error Summary ----------

    ./certbot-auto certonly 报错: Transaction check error:   file /etc/rpm/macros.ghc-srpm from install of ...

  4. CentOS6.5安装ElasticSearch6.2.3

    CentOS6.5安装ElasticSearch6.2.3 1.Elastic 需要 Java 8 环境.(安装步骤:http://www.cnblogs.com/hunttown/p/5450463 ...

  5. Spring IOC容器对bean的生命周期进行管理的过程

    1.通过构造器或者工厂方法创建bean的实例 2.为bean的属性设置值和对其他bean的引用 3.将bean的实例传递给bean的后置处理器BeanPostProcessor的postProcess ...

  6. 设 $y_1(x), y_2(x)$ 是 $y''+p(x)y'+q(x)y=0$ 的两个解 ($p(x), q(x)$ 连续), 且 $y_1(x_0)=y_2(x_0)=0$, $y_1(x)\not\equiv 0$. 试证: $y_1(x)$, $y_2(x)$ 线性相关.

    设 $y_1(x), y_2(x)$ 是 $y''+p(x)y'+q(x)y=0$ 的两个解 ($p(x), q(x)$ 连续), 且 $y_1(x_0)=y_2(x_0)=0$, $y_1(x)\n ...

  7. [再寄小读者之数学篇](2014-06-22 发散级数 [中国科学技术大学2012年高等数学B考研试题])

    设 $a_n>0$, $S_n=a_1+a_2+\cdots+a_n$, 级数 $\dps{\vsm{n}a_n}$ 发散, 证明: $\dps{\vsm{n}\cfrac{a_n}{S_n}} ...

  8. 02 Redis关闭服务报错---(error) ERR Errors trying to SHUTDOWN. Check logs.

    127.0.0.1:6379> shutdown (error) ERR Errors trying to SHUTDOWN. Check logs. 1.在redis.conf中修改日志文件的 ...

  9. java8 list统计(求和、最大、最小、平均)

    list.stream().mapToDouble(User::getHeight).sum()//和 list.stream().mapToDouble(User::getHeight).max() ...

  10. 部署自己的服务器ubuntu

    一直都是在公司的服务器上工作,想搞点自己的idea比较不方便,所以近期租了要给自己的阿里云服务器. 以下为必要的软件的安装流程: jdk+jre: 1.去官网下载 jdk-linux版本: 2.解压压 ...