参考

http://openhome.cc/Gossip/Python/Property.html

http://pyiner.com/2014/03/09/Python-property.html

在Python中property()是一个内建函数,创建并返回一个property对象。函数的定义如下。

property(fget=None, fset=None, fdel=None, doc=None)

fget是获取属性的值的函数,fset是设置属性值的函数,fdel是删除属性的函数,doc是一个字符串(like a comment).从实现来看,这些参数都是可选的,所以可以向下面这样简单的创建一个property对象。

property()
<property object at 0x0000000003239B38>
property有三个方法getter(), setter()和delete() 来指定fget, fset和fdel。

一般写法

class Ball:
def __init__(self, radius):
if radius <= 0:
raise ValueError('必須是正數')
self.__radius = radius def getRadius(self):
return self.__radius def setRadius(self, radius):
self.__radius = radius def delRadius(self):
del self.__radius radius = property(getRadius, setRadius, delRadius, 'radius 特性說明') #output
ball = Ball(1.23)
print ball.radius
ball.radius = 2.31
print ball.radius

装饰器写法

class Ball:
def __init__(self, radius):
if radius <= 0:
raise ValueError('必須是正數')
self.__radius = radius @property
def radius(self):
return self.__radius @radius.setter
def radius(self, radius):
self.__radius = radius @radius.deleter
def radius(self):
del self.__radius

python property用法的更多相关文章

  1. python @property用法(转载)

    偶然碰到一篇讲解 @property 比较清晰的文章 记录下来 日常复习 # @property'''@property是python的一种装饰器,是用来修饰方法的 作用:我们可以使用@propert ...

  2. Python中“*”和“**”的用法 || yield的用法 || ‘$in’和'$nin' || python @property的含义

    一.单星号 * 采用 * 可将列表或元祖中的元素直接取出,作为随机数的上下限: import random a = [1,4] print(random.randrange(*a)) 或者for循环输 ...

  3. python property

    python property 在2.6版本中,添加了一种新的类成员函数的访问方式--property. 原型 class property([fget[, fset[, fdel[, doc]]]] ...

  4. Python高级用法总结

    Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...

  5. python property详解

    Python中有一个被称为属性函数(property)的小概念,它可以做一些有用的事情.在这篇文章中,我们将看到如何能做以下几点: 将类方法转换为只读属性 重新实现一个属性的setter和getter ...

  6. python property装饰器

    直接上代码: #!/usr/bin/python #encoding=utf-8 """ @property 可以将python定义的函数“当做”属性访问,从而提供更加友 ...

  7. python argparse用法总结

    转:python argparse用法总结 1. argparse介绍 argparse是python的一个命令行解析包,非常适合用来编写可读性非常好的程序. 2. 基本用法 prog.py是我在li ...

  8. Python @property 详解

    本文讲解了 Python 的 property 特性,即一种符合 Python 哲学地设置 getter 和 setter 的方式. Python 有一个概念叫做 property,它能让你在 Pyt ...

  9. Anaconda下载及安装及查看安装的Python库用法

    Anaconda下载及安装及查看安装的Python库用法 Anaconda 是一个用于科学计算的 Python 发行版,提供了包管理与环境管理的功能.Anaconda 利用 conda 来进行 pac ...

随机推荐

  1. Struts2学习第七课 result

    result 是action节点的子节点 result 代表action方法执行后,可能去的一个目的地 一个action节点可以配置多个result子节点. result的name属性值对应着acti ...

  2. SCSS 中的 &::before 和 &::after

    在看一个文件的时候,发现有&::before 和 &::after, 没有理解怎么用的,因为以前在项目的css文件中,从来没有使用过,也没有见过 网上查询了一下,才发现&::b ...

  3. Android Market google play store帐号注册方法流程 及发布应用注意事项

    Android Market google play store帐号申请 注册方法流程 在 Google Play 中发布软件之前,您需要完成以下三项工作: 创建开发人员个人资料 接受开发人员分发协议 ...

  4. Git error: unable to create file xxx: Filename too long

    一.问题描述 在使用 git 时,提示 error: unable to create file xxx: Filename too long error: unable to create file ...

  5. Spark BlockManager 概述

    Application 启动的时候: 1. 会在 SparkEnv 中实例化 BlockManagerMaster 和 MapOutputTracker,其中 (a) BlockManagerMast ...

  6. 2017-10-6 清北刷题冲刺班a.m

    1.角谷猜想 #include<iostream> #include<cstdio> #include<cstring> #define maxn 10010 us ...

  7. uuid安装 插件安装

    yum -y install uuid uuid-devel 安装uuid包tar -zxvf uuid-1.6.1.tar.gzcd uuid-1.6.1./configuremakemake in ...

  8. docker下载安装教程(Linux系统)

    原文链接:http://www.studyshare.cn/blog-front//software/details/1160/0一.检查 1.检查安装的docker 命令:yum list inst ...

  9. react native ios打包,即生产包

    参考文章:http://www.devio.org/2017/02/09/React-Native%E5%8F%91%E5%B8%83APP%E4%B9%8B%E6%89%93%E5%8C%85iOS ...

  10. shiro 重定向 后 带有 sessionId 的 解决 办法

    http://blog.csdn.net/aofavx/article/details/51701012