用法一

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用法的更多相关文章

  1. @property用法总结

    1.当方法需要传入别的参数时,不能定义成@property. 比如_table(self, owner)

  2. python property用法

    参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...

  3. property用法,使Python中的get方法和set方法使用更简单

    方法一: class a: def __init__(self): self.__num = 1              #定义一个私有变量(以双下划线开头的是私有变量) def getNum(se ...

  4. python @property用法(转载)

    偶然碰到一篇讲解 @property 比较清晰的文章 记录下来 日常复习 # @property'''@property是python的一种装饰器,是用来修饰方法的 作用:我们可以使用@propert ...

  5. property

    一.property用法 property(fget=None, fset=None, fdel=None, doc=None) -> property attribute fget is a ...

  6. OC点语法介绍和使用以及@property关键字

    使用"点语法" Person *p =[Person new]; //点语法 //对象.属性名 //注意,此时 (p.age)并不是直接方法实例对象 //而是xcode可能到点语法 ...

  7. day28-python之property

    1.property用法 # class Goods: # def __init__(self): # # 原价 # self.original_price = 100 # # 折扣 # self.d ...

  8. Python学习第十六课——静态属性(property, classmethod, staticmethod)

    计算所居住房子的面积 普通写法 class Room: def __init__(self,name,owner,width,length,heigh): self.name=name self.ow ...

  9. Python之@property详解及底层实现介绍

    转自:https://blog.csdn.net/weixin_42681866/article/details/83376484 前文 Python内置有三大装饰器:@staticmethod(静态 ...

随机推荐

  1. Android 回调函数的理解,实用简单(回调函数其实是为传递数据)

    作者: 夏至,欢饮转载,也请保留这段申明 http://blog.csdn.net/u011418943/article/details/60139910 一般我们在不同的应用传递数据,比较方便的是用 ...

  2. 『TensorFlow』模型保存和载入方法汇总

    『TensorFlow』第七弹_保存&载入会话_霸王回马 一.TensorFlow常规模型加载方法 保存模型 tf.train.Saver()类,.save(sess, ckpt文件目录)方法 ...

  3. 在线自动创建springboot工程

    https://start.spring.io/

  4. Win10系列:C#应用控件基础23

    Telerik UI Controls for Windows 8 Telerik UI Controls for Windows 8是一套为创建Windows UWP应用而设计的工具集,开发人员可以 ...

  5. Unity中UGUI鼠标穿透UI问题的解决方法

    不过在使用时需要先获取两个红色显示的变量,graphicRaycaster和eventSystem. 这两个变量分别对应的是Canvas中的GraphicRaycaster组件和创建UI时自动生成的“ ...

  6. python全栈开发笔记---------数据类型-----字典dict

    字典 #dict #1.基本结构 info= { "k1" : "v1", #键值对 "k2" : "v2" } ### ...

  7. flask 自动切换环境

    简介: 我就是个半吊子程序员.在单位写点程序,在家也写点程序. 单位是企业网,不能上互联网,家里也没办法连上企业网,没有VPN. 主武器是我的笔记本电脑.在单位有一台淘汰的linux服务器,家里有个N ...

  8. Android开发 ---基本UI组件6 :只定义一个listView组件,然后通过BaseAdapter适配器根据数据的多少自行添加多个ListView显示数据

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  9. python 图像转矩阵,矩阵转图像

    1.图像转换为矩阵 matrix = numpy.asarray(image) Help on function asarray in module numpy.core.numeric: asarr ...

  10. web 页面上纯js实现按钮倒计数功能(实时计时器也可以)

    需求构思:本功能想实现的是,一个按钮在页面载入就显示提醒续费,,,倒数60秒后,完成提醒功能,可以按另外一个页面跳转到主页. 参考网上的大神,实现如下:Button2倒数,Button3跳转,在页面上 ...