一、从@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. BOOST_PREVENT_MACRO_SUBSTITUTION

    [BOOST_PREVENT_MACRO_SUBSTITUTION] 用于防止函数被macro替换的问题. 例如: 参考: 1.https://blog.csdn.net/yanxiangtianji ...

  2. yii2-redis 扩展详解

    安装yii2-redis composer require yiisoft/yii2-redis 修改config/web.php 的  components 配置 'cache' => [ / ...

  3. flutter Dialog里ListView的问题

    showDialog( context: context, builder: (ctx) { return // Dialog( // child: Container( // padding: Ed ...

  4. TZOJ 2703 Cow Digit Game(sg博弈)

    描述 Bessie is playing a number game against Farmer John, and she wants you to help her achieve victor ...

  5. route的简单使用

    route [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If] add : 添加一条路由规则del : 删除一条路由规则-net ...

  6. openstack系列文章(2)dashboard

    玩转dashboard之前,考虑一些事情:(1)安全问题:网络访问策略(2)镜像的密码管理:windows或者linux,root或者administrator密码怎么管理(3)怎样创建自己的镜像:w ...

  7. 关于接口测试工具postman与DHC介绍

    一.Postman背景介绍 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具.今天给大家介 ...

  8. Markdown使用小总结[不定时更新]

    title: Markdown使用小总结 date: 2019-03-27 10:09:19 tags: Markdown --- 鸽了这么久,Markdown使用下降,因此写一篇博客来总结一下至今( ...

  9. 记一次mac下使用mamp集成环境配置lumen项目自定义域名遇到的花样问题

    1.安装好mamp集成环境,自行百度. 2.从公司项目版本库里将项目克隆到本地. 好了,开始配置自定义域名来访问项目,以下是遇到的问题集锦... 1.web服务器使用的nginx,配置完域名访问报40 ...

  10. 201771010134杨其菊《面向对象程序设计java》第十周学习总结

    第8章泛型程序设计学习总结 第一部分:理论知识 主要内容:   什么是泛型程序设计                   泛型类的声明及实例化的方法               泛型方法的定义      ...