python中property简单使用与实现
property简单使用
class P:
"""
property简单使用
"""
def __init__(self,name):
self._name = name @property
def name(self):
return self._name @name.setter
def name(self,val):
self._name = val @name.deleter
def name(self):
del self._name p = P("zhangsan")
print(p.name)
print(p.__dict__)
p.name = "wangsi"
print(p.name)
print(p.__dict__)
结果:
zhangsan
{'_name': 'zhangsan'}
wangsi
{'_name': 'wangsi'}
property简单实现
class Property: def __init__(self,fget=None,fset=None,fdel=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
pass def __set__(self, instance, value):
self.fset(instance,value)
return self def __get__(self, instance, owner):
if not instance:
return self
return self.fget(instance) def __delete__(self, instance):
self.fdel(instance)
return self def setter(self,fn):
#保留设置函数的副本
self.fset = fn
return self def deleter(self,fn):
# 保留删除函数的副本
self.fdel = fn
return self class Person: # d = Property()
def __init__(self,data):
self._data = data @Property #等价式 data=Propery(data)
def data(self):
return self._data @data.setter #等价式 data=data.setter(data)
def data(self,val):
self._data = val @data.deleter #等价式 data=data.deleter(data)
def data(self):
del self._data p = Person(19)
p.data = 123
print(Person.__dict__)
print(p.__dict__)
del p.data
print(Person.__dict__)
print(p.__dict__)
结果:
{'__init__': <function Person.__init__ at 0x0000015F3F0F0598>, '__dict__': <attribute '__dict__' of 'Person' objects>, 'data': <__main__.Property object at 0x0000015F3F0FE240>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__module__': '__main__', '__doc__': None}
{'_data': 123}
{'__init__': <function Person.__init__ at 0x0000015F3F0F0598>, '__dict__': <attribute '__dict__' of 'Person' objects>, 'data': <__main__.Property object at 0x0000015F3F0FE240>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__module__': '__main__', '__doc__': None}
{}
注意:property 依赖装饰器和数据描述器实现
数据描述器:需要实现__set__()方法和__get__()方法
非数据描述器:需实现__get__()方法
python中property简单使用与实现的更多相关文章
- python 中property函数如何实现
实际上,在python中property(fget,fset,fdel,doc)函数不是一个真正的函数,他其实是拥有很多特殊方法的类. 这特殊类总的很多方法完成了property函数中的所有工作,涉及 ...
- python中@property装饰器的使用
目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...
- python 中 property 属性的讲解及应用
Python中property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回 property属性的有两种方式: 1. 装饰器 即:在方法上应用装饰器 2. 类属性 即 ...
- python中一个简单的webserver
python中一个简单的webserver 2013-02-24 15:37:49 分类: Python/Ruby 支持多线程的webserver 1 2 3 4 5 6 7 8 9 10 11 ...
- Python 中lambda 简单介绍
转自:https://www.cnblogs.com/AlwaysWIN/p/6202320.html 在学习python的过程中,lambda的语法经常出现,现在将它整理一下,以备日后查看. 1.l ...
- 转载-对于Python中@property的理解和使用
原文链接:https://blog.csdn.net/u013205877/article/details/77804137 重看狗书,看到对User表定义的时候有下面两行 @property def ...
- python中property属性的介绍及其应用
Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回. 使用property修饰的实例方法被调用时,可以把它当做实例属性一样 property的 ...
- 正则表达式在python中的简单使用
正则表达式独立与编程语言,基本上所有的编程语言都实现了正则表达式的相关操作.在Python中正则表达式的表现为re模块: import re 其操作有三个方法: my_string = "h ...
- 对于Python中@property的理解和使用
@property 这个我们在很多代码中都会用到,简单讲即为一个只读属性的调用 如果需要有修改权限,需要再加一个@属性名.setter 例: #!/usr/bin/env python # -*- c ...
随机推荐
- CUDA -- 规约求矩阵的行和
求矩阵每行的和? 可以把每行放入一个不同线程块,这样行与行之间进行粗粒度的并行.而对于每行,其对应的线程块中分配n个线程(对应行宽),使用共享存储器,让每个线程从显存中读取一个数至shared mem ...
- c# 第39节 抽象类、抽象方法
本节内容: 1:抽象类的说明 2:抽象类的实例 1:抽象类的说明 抽象类定义:方法前有abstract就称为抽象类.抽象方法,抽象方法不提供任何实际实现. 注意点1: 抽象方法必须在抽象类中声明: 不 ...
- python使用open的OSError: [Errno 22] Invalid argument错误
这两天在写一个新闻类的spider时,遇到了OSError: [Errno 22] Invalid argument这个错误,苦恼的两天,无果.后来通过请教学长,发现原来是打开的文件名中含有一些系统的 ...
- Python tempfile (临时文件)
Python tempfile 大量临时数据放在内存中会占用大量资源,可以使用临时文件来进行储存 临时文件不用命名,且使用后会被自动删除 TemporaryFile 使用 TemporaryFile ...
- VMWare虚拟机提示:另一个程序已锁定文件的一部分,打不开磁盘...模块"Disk"启动失败的解决办法
重启了电脑之后,打开VMware就发现出现了“锁定文件失败,打不开磁盘......模块"Disk"启动失败.”这些文字 为什么会出现这种问题: 这是因为虚拟机在运行的时候,会锁定文 ...
- layui实现分页
一 准备工作 首先必须先引入layui的完整目录,也就是你下载下来的整个layui的目录都要放在你的资源文件夹下,也就是这个文件目录 刚开始接触layui的时候,以为和jquery,vue等框架一样, ...
- 解决office365无法登录以及同步的问题
解决office365无法登录以及同步的问题 You better need to test them one by one. You better need to test them one by ...
- pytest 常见用法
前言 之前一篇文章简单介绍了 pytest 以及 fixture :https://www.cnblogs.com/shenh/p/11572657.html .实际在写自动化测试脚本中,还会有一些很 ...
- python asyncio 关闭task
import asyncio import time async def get_html(sleep_times): print("waiting") await asyncio ...
- Date以及LocalDateTime格式化
public static void main(String[] args) { LocalDateTime local = LocalDateTime.now(); Date date = new ...