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 ...
随机推荐
- 谈谈你对OOM的理解?
(1)整体架构 (1)ByteBuffer使用native方法,直接在堆外分配内存. 当堆外内存(也即本地物理内存)不够时,就会抛出这个异常 ----GC Direct buffer memo ...
- 解决汉化pycharme之后设置打不开的问题
首先进入安装pycharme目录下lib目录下,将汉化包移出去,只留下英文包 然后打开pycharme即可打开设置 在你改完设置之后,可以再将汉化包放进来 英文包:https://pan.baidu. ...
- c# WF 第4节 窗体的事件
本节内容: 1:事件是什么? 2:窗体事件在哪可以找到 3:事件有哪些 1:事件是什么? 2:窗体事件在哪里 第一种: 第二种: 3:事件有哪些
- [C5] Andrew Ng - Structuring Machine Learning Projects
About this Course You will learn how to build a successful machine learning project. If you aspire t ...
- C++ trais技术 模板特化的应用
// traits 的应用 /////////////////////////////////////////// // traits template <typename T> clas ...
- Special-Judge模板
SPJ模板 放一篇\(SPJ\)(\(Special-Judge\))的模板. 注意,仅适用于\(Lemon\). 并不适用于洛谷. 代码:@zcs0724 #include <bits/std ...
- pointcnn
这篇论文先举例子解释了为什么卷积无法直接应用在点云数据上. 如图1, 传统的卷积是作用在2维图像数据上.图像中每个像素的顺序是固定的,也就是说数据是结构化存储的.直接使用conv2d就能从这种潜在的空 ...
- 《阿里B2B技术架构演进详解》----阅读
B2B(Business To Business)是指一个市场的领域的一种,是企业对企业之间的营销关系.先来总结一下阿里B2B共分为三个阶段: 第一阶段,建立信息网站提供信息和营销服务平台,让买家更加 ...
- Velocity与Jsp、Freemarker的对比
在java领域,表现层技术主要有三种:jsp.freemarker.velocity.jsp是大家最熟悉的技术 优点: 1.功能强大,可以写java代码 2.支持jsp标签(jsp tag) 3.支持 ...
- go 创建切片
package main import "fmt" func main() { //自动推导类型,同时进行初始化 s1 := [],,,} fmt.Println("s1 ...