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 ...
随机推荐
- -shared -fPIC
gcc -shared -fPIC -o 1.so 1.c 这里有一个-fPIC参数 PIC就是position independent code PIC使.so文件的代码段变为真正意义上的共享
- Appium滑动函数:Swipe()
Appium处理滑动方法是swipe 滑动API:Swipe(int start x,int start y,int end x,int y,duration) 解释: int start x-开始滑 ...
- logistic 回归(线性和非线性)
一:线性logistic 回归 代码如下: import numpy as np import pandas as pd import matplotlib.pyplot as plt import ...
- c# 第40节 密封类、密封方法
本节内容: 1:密封的存在意义 2:密封方法的实现 1:密封的存在意义 2:密封方法的实现 class2::父类 class Class2 { public virtual void show() { ...
- 动态添加Redis密码认证的方法
1.定制jedis 对redis返回的错误的处理,做两处修改: 忽略 (error) ERR Client sent AUTH, but no password is set.使配置了密码的jedis ...
- pytroch 权重初始化和加载词向量的方法
1.几种不同的初始化方法 import torch.nn as nn embedding = torch.Tensor(3, 5) #如下6种初始化方法 #正态分布 nn.init.normal_(e ...
- Vue 中的 子组件 给 父组件 传值
子组件的某一个事件(sendData)内部,通过this.$emit('事件名', 传递的值)传递数据父组件在调用子组件的地方,绑定了子组件自定义的 事件名(change-data) 定义父组件的地方 ...
- 小白专场-树的同构-python语言实现
目录 一.题意理解 二.求解思路 更新.更全的<数据结构与算法>的更新网站,更有python.go.人工智能教学等着你:<https://www.cnblogs.com/nickch ...
- Comment file
/// This is the head comment of a file. /*********************************************************** ...
- Sitecore 8.2 渠道简介
渠道是联系人通过广告系列或面对面与您的品牌互动时所使用的路径.联系人可以通过手机上的应用与您的品牌互动,点击社交网络上的广告访问您的网站,或访问实体店购买商品.使用Sitecore体验平台,您可以使用 ...