一、从@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. mysql批量update更新,mybatis中批量更新操作

    在日常开发中,有时候会遇到批量更新操作,这时候最普通的写法就是循环遍历,然后一条一条地进行update操作.但是不管是在服务端进行遍历,还是在sql代码中进行遍历,都很耗费资源,而且性能比较差,容易造 ...

  2. Python 的xlutils模块

    python模块: xlrd:读取excel xlwt:写入excel 缺点:excel格式无法复用 推荐xlutils模块 可复制原excel格式 from xlutils.copy import ...

  3. 20165315 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1

    20165315 2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 一.安装过程 1.基本配置 创建一个新的自定义vm 选择创建自定虚拟机 操作系统选择" ...

  4. 分布式系统-主键唯一id,订单编号生成-雪花算法-SnowFlake

    分布式系统下 我们每台设备(分布式系统-独立的应用空间-或者docker环境) * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作 ...

  5. jq 字符串去除空格

    1.去除首尾空格: var txt = $('#Txt').val().trim(); txt = txt.replace(/(^\s*)|(\s*$)/g, ""); 2.去除所 ...

  6. Centos 7创建一个服务

    首先创建服务文件 vim /etc/systemd/system/node.service #内容如下 [Unit] Description=ethereum-go Monitor Daemon Af ...

  7. 苹果手机input有圆角阴影的解决方法

    input[type=button], input[type=submit], input[type=file], button { cursor: pointer; -webkit-appearan ...

  8. Vue添加jquer插件

    一.现象 综合开发需要,需要引用使用 二.解决 1.先安装jquer插件,命令运行: npm i jquery --save-dev (tips:  i  也就是 install --save-dev ...

  9. 探索未知种族之osg类生物---渲染遍历之裁剪二

    前言 上一节我们大致上过了一遍sceneView::cull()函数,通过研究,我们发现上图中的这一部分的代码才是整个cull过程的核心部分.所以今天我们来仔细的研究一下这一部分. sceneView ...

  10. SpringCloud(一)Eureka注册中心

    Eureka简介 Eureka作为注册中心,管理各种服务功能包括服务的注册.发现.熔断.负载.降级等 Eureka注册中心实例 Eureka Server 1.pom文件配置SpringBoot.Sp ...