property用法
用法一
class Test(object):
def __init__(self):
self.__Num = 100 def setNum(self,Num):
print("---set---")
self.__Num = Num def getNum(self):
return self.__Num num = property(getNum,setNum) t = Test()
print("##########1")
print(t.num) #相当于调用了t.getNum()
print("##########2")
t.num = 200 #相当于调用了t.setNum(200)
print("##########3")
print(t.num)
print("##########4")
输出
##########1
100
##########2
---set---
##########3
200
##########4
用法二
class Test(object):
def __init__(self):
self.__Num = 100 @property
def num(self):
return self.__Num @num.setter
def num(self,Num):
print("---set---")
self.__Num = Num t = Test()
print("##########1")
print(t.num)
print("##########2")
t.num = 200
print("##########3")
print(t.num)
print("##########4")
输出
##########1
100
##########2
---set---
##########3
200
##########4
property用法的更多相关文章
- @property用法总结
1.当方法需要传入别的参数时,不能定义成@property. 比如_table(self, owner)
- python property用法
参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...
- property用法,使Python中的get方法和set方法使用更简单
方法一: class a: def __init__(self): self.__num = 1 #定义一个私有变量(以双下划线开头的是私有变量) def getNum(se ...
- python @property用法(转载)
偶然碰到一篇讲解 @property 比较清晰的文章 记录下来 日常复习 # @property'''@property是python的一种装饰器,是用来修饰方法的 作用:我们可以使用@propert ...
- property
一.property用法 property(fget=None, fset=None, fdel=None, doc=None) -> property attribute fget is a ...
- OC点语法介绍和使用以及@property关键字
使用"点语法" Person *p =[Person new]; //点语法 //对象.属性名 //注意,此时 (p.age)并不是直接方法实例对象 //而是xcode可能到点语法 ...
- day28-python之property
1.property用法 # class Goods: # def __init__(self): # # 原价 # self.original_price = 100 # # 折扣 # self.d ...
- Python学习第十六课——静态属性(property, classmethod, staticmethod)
计算所居住房子的面积 普通写法 class Room: def __init__(self,name,owner,width,length,heigh): self.name=name self.ow ...
- Python之@property详解及底层实现介绍
转自:https://blog.csdn.net/weixin_42681866/article/details/83376484 前文 Python内置有三大装饰器:@staticmethod(静态 ...
随机推荐
- 『TensorFlow』pad图片
tf.pad()文档如下, pad(tensor, paddings, mode='CONSTANT', name=None, constant_values=0) Pads a tensor. ...
- PIL合并4张图demo 800px以下的居中显示小例子
from PIL import Image #新建一个空白文件 大小为1600*1600 颜色为白色 newIm= Image.new('RGB', (1600, 1600), 'white') #打 ...
- Eclipse+Spring boot开发教程
一.安装 其实spring boot官方已经提供了用于开发spring boot的定制版eclipse(STS,Spring Tool Suite)直接下载使用即可,但考虑到可能有些小伙伴不想又多装个 ...
- 算法-最通俗易懂的KMP算法详解
有些算法,适合从它产生的动机,如何设计与解决问题这样正向地去介绍.但KMP算法真的不适合这样去学.最好的办法是先搞清楚它所用的数据结构是什么,再搞清楚怎么用,最后为什么的问题就会有恍然大悟的感觉.我试 ...
- SSM 框架 整合<SpringMVC+Spring+MyBatis>
一 框架的搭建1.建立一个maven项目 2.建立五个module(entity,dao,service,action,web-view) 3.给予它们之间的依赖关系 dao-->entity ...
- converting the moment tensor to strie-dip-rake
在多断层求解的试验中,用到了六个基本矩张量: 而显然,尚不能从图中直接读出strke,dip,rake的值,但有关资料给出了这六个基本矩张量的momet tensor: 而找到一个网站可以方便地将mo ...
- linux中ls -l介绍
[root@localhost ~]# ls -l 总计 152 -rw-r--r-- 1 root root 2915 08-03 06:16 a -rw------- 1 root root 10 ...
- ela的UNASSIGNED索引修复
1.查找UNASSIGNED未分片的索引: #curl -s "http://localhost:9200/_cat/shards" -u username:passwd | gr ...
- redis设置过期时间
- 在Power BI报表和仪表板中显示刷新日期\时间
有人最近问我:“如何在报告和仪表板中显示最后刷新数据的日期和时间?”这里有两个简单的技巧在这分享下,也许可以帮助到你. 显示上次刷新日期\时间 要想显示刷新的日期和时间,我们需要在模型本身中存储时间刷 ...