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(静态 ...
随机推荐
- [spring源码] 小白级别的源码解析(一)
一直都在用spring,但是每次一遇到spring深入的问题,就是比较懵的状态.最近花了段时间学习了一下spring源码. 1,spring版本介绍 虽然工作中,一直在用到spring,可能有时候,并 ...
- webForm TO MVC
- mysql 取年、月、日、时间
select id, phone,time,year(time),month(time), DAY(time),TIME(time) from user where phone='xxxxxx' #分 ...
- html5(一)
HTML5 三个基本特色:结构.样式.功能. <!DOCTYPE html ><html lang="en"><head> <meta c ...
- vim 插件 -- omnicppcomplete
omnicppcomplete 插件是基于ctags来实现补全的.所以,要先安装好ctags才可以使用. 下载 https://www.vim.org/scripts/script.php?scrip ...
- test pthread code
#include <iostream> #include <pthread.h> using namespace std; ; void * add(void *); pthr ...
- jsp请求转发与重定向区别小结
1.当使用转发时,JSP容器将使用一个内部方法来调用目标页面,新的页面继续处理同一个请求,而浏览器不会知道这个过程; 2.重定向是第一个页面通知浏览器发送一个新的页面请求. 3.转发不改变URL,重定 ...
- spring boot Tomcat文件上传找不到零时文件夹
springboot项目上传文件是找不到零时文件夹 1.本身启动jar包时内置Tomcat没有创建零时文件夹 2.在zuul网关级别没有创建零时文件夹 处理方案: -Djava.io.tmpdir=/ ...
- VM for Linux 版本的Bundle格式文件的安装
VM for Linux 版本的安装步骤: 下面链接下载VM程序包 : https://www.vmware.com/products/workstation-pro/workstation-pro- ...
- 读取txt数据存入数据库中
http://blog.csdn.net/daditao/article/details/18899469