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 ...
随机推荐
- RAID10(5块硬盘)的简介和创建
一. RAID10简介 (1)兼具速度和安全性,但成本很高. (2)继承了RAID0的快速与RAID1的安全,RAID1在这里提供了冗余备份的阵列,而RAID0则负责数据的读写阵列.因这 ...
- C#所有经典排序算法汇总
1.选择排序 选择排序 class SelectionSorter { private int min; public void Sort(int[] arr) ...
- 如何调用.so动态库中的函数,如何把自己的函数导出为.so的动态库函数供别人调用
调用.so中的函数和平常的函数没有区别,只是在编译连接时加上-lxxxx就行了.要生成.so库,则编译时用下面的语句:gcc -shared -Wl,-soname,libmyfun.so -o li ...
- (转)idea创建Maven项目时Maven插件内看不到mybatis-generator
转载地址:https://blog.csdn.net/yytwiligt/article/details/81010360 创建Maven项目时插件配置添加了mybatis-generator但是右侧 ...
- 深度工作(Charlotte)
作者:Charlotte文章转自:https://zhuanlan.zhihu.com/p/56719363 为什么要深度工作? 作为一名程序员/媛,工作时长动则十几小时,然而有的人效率奇高,有的人上 ...
- nuxtjs踩坑指南
1.nuxt引入问题:Can't resolve 'stylus-loader' 原因在于没有安装stylus,安装即可:npm install stylus stylus-loader --save ...
- [LOJ 2721][UOJ 396][BZOJ 5418][NOI 2018]屠龙勇士
[LOJ 2721][UOJ 396][BZOJ 5418][NOI 2018]屠龙勇士 题意 题面好啰嗦啊直接粘LOJ题面好了 小 D 最近在网上发现了一款小游戏.游戏的规则如下: 游戏的目标是按照 ...
- 【前端知识体系-JS相关】ES6专题系列总结
1.如何搭建ES6的webpack开发环境? 安装Node环境 node -v // 10.14.1 安装NPM环境 npm -v // 6.4.1 安装babel npm install @babe ...
- springboot只能一个main方法解决办法
pom.xml修改properties,增加这行 <start-class>com.eshore.main.SpringBootStarter</start-class> 或者 ...
- MySQL性能诊断与调优
LAMP 系统性能调优,第 3 部分: MySQL 服务器调优http://www.ibm.com/developerworks/cn/linux/l-tune-lamp-3.html LoadRun ...