一、从@porperty说起

  Python内置的@property装饰器是负责把一个方法变成属性调用的

class Stu(object):
def __init__(self,age):
self.__age=age
@property #直接调用属性
def birth(self):
return self._age
@birth.setter #设置属性
def birth(self, value):
self.__age = value
@birth.deleter #删除属性
def birth(self):
del self.__age #调用方法为
stu_a = Stu(3)
stu_a.birth # >>> 3 调用@property修饰的方法
stu_a.birth = 4 >>> 调用@setter修饰的方法
del stu_a.birth >>调用@deleter修饰的方法

二、property类

事实上property是一个类,里面封装了property,setter,deleter等方法,其中__init__()构造函数中,是存在4个参数的!

def __init__(self, fget=None, fset=None, fdel=None, doc=None):
pass

这也就是property的另一种用法:property()

class Stu(object):
def __init__(self,age):
self.__age=age
def get_age(self):
return self._age
def set_age(self, value):
self.__age = value
def del_age(self):
del self.__age
gsd = property(get_age, set_age, del_age) stu1 = Stu(1)
#调用方式:
stu1.gsd
stu1.gsd = 2
del stu1.gsd

三、自己的@property

@property其实就是通过描述符来定制,虽然内置方法为c封装的,但可以用python代码简单的实现@property的功能:

class B:  # property对象
def __init__(self, a): # 这里传入的是aa方法
self.a = func def __get__(self, instance, owner):
ret = self.func(instance) # instance为A的实例对象,即aa()函数需要的self
return ret class A:
@B # B = B(aa)
def aa(self):
return "property" b = A()
print(b.aa)

python property对象的更多相关文章

  1. python property理解

    一般情况下我这样使用property: @property def foo(self): return self._foo # 下面的两个decrator由@property创建 @foo.sette ...

  2. python面相对象进阶

    1. 类的成员 python 类的成员有三种:字段.方法.属性 字段 字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同, 普通字段 属于对象,只有对象创建 ...

  3. python property装饰器

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

  4. Python @property 详解

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

  5. Fluent Python: @property

    Fluent Python 9.6节讲到hashable Class, 为了使Vector2d类可散列,有以下条件: (1)实现__hash__方法 (2)实现__eq__方法 (3)让Vector2 ...

  6. python property用法

    参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...

  7. Python - 面对对象(进阶)

    目录 Python - 面对对象(进阶) 类的成员 一. 字段 二. 方法 三. 属性 类的修饰符 类的特殊成员 Python - 面对对象(进阶) 类的成员 一. 字段 字段包括:普通字段和静态字段 ...

  8. 小学生绞尽脑汁也学不会的python(面对对象-----成员)

    小学生绞尽脑汁也学不会的python(面对对象-----成员) 成员 class Person: def __init__(self, name, num, gender, birthday): # ...

  9. 16、python面对对象之类和继承

    前言:本文主要介绍python面对对象中的类和继承,包括类方法.静态方法.只读属性.继承等. 一.类方法 1.类方法定义 使用装饰器@classmethod装饰,且第一个参数必须是当前类对象,该参数名 ...

随机推荐

  1. xml嵌套防止解析

    举个例子 <?xml version="1.0" encoding="UTF-8"?><Messages><Message typ ...

  2. 走进MyBatis的世界

    1.MyBatis可框架及ORM 1.Mybatis框架简介 MyBatis是一个开源的数据持久层框架.它内部封装了通过了JDBC访问数据库的操作,支持普通的SQL查询,存储过程和高级映射,几乎消除了 ...

  3. 我们一起踩过的坑----react(antd)(一)

    1.}]          && ){             ){ ){ ||){ ){ );); , }; }); }, beforeUpload: (file) => { ...

  4. leetcode 动态规划类型题

    1,Triangle int mininumTotal(vector<vector<int>>& triangle) { ; i >= ; --i) { ; j ...

  5. python3 urllib 类

    urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google ...

  6. 矢量水听器 近场 远场 --------------------常规波束形成,MVDR的比较

    摘自<水平线阵的反卷积常规波束形成>,IRONMAN--------------------------------------------------- 在常规的波束形成中,是将阵列上的 ...

  7. Flask使用记录

    关于FLASK框架的使用 使用pycharm创建工程 在默认的templates中新增模板页面 在默认的app.py中定义路由并引用模板 @app.route("/add", me ...

  8. Python2和3版本对str和bytes类型的处理

    python2中字符串分为2种类型: 字节类型:str,字节类型,通过decode()转化为unicode类型 unicode类型:unicode ,通过encode转化为str字节类型 字节类型 和 ...

  9. node.js中对 redis 的安装和基本操作

    一.win下安装redis https://github.com/MicrosoftArchive/redis/releases 下载Redis-x64-3.2.100.zip,然后解压,放到自定义目 ...

  10. MongoDB及Mongoose的记录

    MongoDB是一种NoSQL的文档型数据库,其存储的文档类型都是JSON对象. 在node.js中由于代码都是异步执行,且nosql也没有“事物”这一定义,所以日常使用中很难保证数据库操作的原子性. ...