用法一

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. css实现0.5px

    使用缩放来0.5来实现,注意兼容各种浏览器 .frz-list li:after { content: ''; position: absolute; bottom:; height: 1px; wi ...

  2. flask配置文件的几种方法

    配置文件的参数 flask中的配置文件是一个flask.config.Config对象(继承字典),默认配置为: { 'DEBUG': get_debug_flag(default=False), 是 ...

  3. JavaScript(ES6)学习笔记-Set和Map数据结构(一)

    一.Set 1.ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. , , , , ']); s; // ...

  4. SVN客户端使用

    1.在SVN服务器添加新的用户,复制SVN URL(路径/目录). 2.在客户端电脑上下载安装SVN客户端,配置hosts文件中的内容和SVN服务器的hosts文件内容一致. hosts路径:C:\W ...

  5. sql查询练习

    #建学生信息表student create table student ( sno varchar(20) not null primary key, sname varchar(20) not nu ...

  6. 微信小程序des加密、PHP des解密

    最近在做对小程序传输数据进行加密,加密方法有很多,使用的是des对称加密 采用的是CBC模式, 引用的插件为tripledes.js,https://github.com/Favour1111in/h ...

  7. Javascript获取图片原始宽度和高度的方法详解

    前言 网上关于利用Javascript获取图片原始宽度和高度的方法有很多,本文将再次给大家谈谈这个问题,或许会对一些人能有所帮助. 方法详解 页面中的img元素,想要获取它的原始尺寸,以宽度为例,可能 ...

  8. JS类小功能

    工作中,总是要处理一些前端的小功能.都是网上搜的JS脚本 <script> //防止页面后退 history.pushState(null, null, document.URL); wi ...

  9. C#磁性窗体设计

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  10. setfacl语法

    1.setfacl的用途setfacl命令可以用来细分linux下的文件权限. chmod命令可以把文件权限分为u,g,o三个组,而setfacl可以对每一个文件或目录设置更精确的文件权限. 换句话说 ...