python property对象
一、从@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对象的更多相关文章
- python property理解
一般情况下我这样使用property: @property def foo(self): return self._foo # 下面的两个decrator由@property创建 @foo.sette ...
- python面相对象进阶
1. 类的成员 python 类的成员有三种:字段.方法.属性 字段 字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同, 普通字段 属于对象,只有对象创建 ...
- python property装饰器
直接上代码: #!/usr/bin/python #encoding=utf-8 """ @property 可以将python定义的函数“当做”属性访问,从而提供更加友 ...
- Python @property 详解
本文讲解了 Python 的 property 特性,即一种符合 Python 哲学地设置 getter 和 setter 的方式. Python 有一个概念叫做 property,它能让你在 Pyt ...
- Fluent Python: @property
Fluent Python 9.6节讲到hashable Class, 为了使Vector2d类可散列,有以下条件: (1)实现__hash__方法 (2)实现__eq__方法 (3)让Vector2 ...
- python property用法
参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...
- Python - 面对对象(进阶)
目录 Python - 面对对象(进阶) 类的成员 一. 字段 二. 方法 三. 属性 类的修饰符 类的特殊成员 Python - 面对对象(进阶) 类的成员 一. 字段 字段包括:普通字段和静态字段 ...
- 小学生绞尽脑汁也学不会的python(面对对象-----成员)
小学生绞尽脑汁也学不会的python(面对对象-----成员) 成员 class Person: def __init__(self, name, num, gender, birthday): # ...
- 16、python面对对象之类和继承
前言:本文主要介绍python面对对象中的类和继承,包括类方法.静态方法.只读属性.继承等. 一.类方法 1.类方法定义 使用装饰器@classmethod装饰,且第一个参数必须是当前类对象,该参数名 ...
随机推荐
- Mysql JSON字段提取某一个属性值的函数
mysql从5.7开始才支持JSON_EXTRACT等 JSON相关的函数, 项目里用到的mysql是5.6的,需要提取JSON字段里某一个属性值进行统计, 自己写了一个笨的提取方法: CREATE ...
- Selenium Grid和IE /Firefox各种填坑
使用selenium grid的步骤 1.确保hub和node都安装并且配置好了java jdk. 2.在hub上运行以下命令. java -jar C:\Software\selenium\sele ...
- CSRF 和 XSS 的区别
XSS 利用的是用户对指定网站的信任,CSRF 利用的是网站对用户网页浏览器的信任 XSS: 跨站脚本攻击 原名为Cross Site Scriptin,为避免和网页层级样式表概念混淆, 另名为XSS ...
- Vue父子组件生命过程
加载渲染过程 父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount-& ...
- linux之egrep命令
1.介绍 egrep工具是grep工具的扩展,相当于grep -E 2.用法 查找1个或1个以上前面的字符为例 查找0个或1个前面字符 egrep 'o?' 1.txt 匹配roo或者body egr ...
- 认识socket
socket socket也称套接字,网络编程的基础.一般情况下我不喜欢直接去说socket的函数都是怎么用的,那个很多人都写出来了,而且肯定比我好的有的是. 但是今天想写的是我的理解中,产生sock ...
- css动画特效
<html> <head> <meta charset="utf-8" /> <title>6种css3鼠标滑过动画效果</t ...
- idea连接mysql
https://blog.csdn.net/Golden_soft/article/details/80952243
- ES查询-term VS match (转)
原文地址:https://blog.csdn.net/sxf_123456/article/details/78845437 elasticsearch 中term与match区别 term是精确查询 ...
- winform使用PrintDocument控件打印以及oledb驱动
代码,需要加入的控件:PrintDocument.PageSetupDialog.PrintDialog.PrintPreviewDialog.BackgroundWorker,控件的Document ...