函数property

  1.为了保护属性,不让它随意的被更改(a.width=xxx)(起码,要符合某些条件),所以我们引入了set和get方法,虽然这个需要自定义(如下图的set_size,get_size方法)。

  >>> class Rectangle:

  ... def __init__(self):

  ... self.width=0

  ... self.height=0

  ... def set_size(self,size):

  ... self.width,self.height=size

  ... def get_size(self):

  ... return self.width,self.height

  ...

  >>> r=Rectangle()

  >>> r.width=10

  >>> r.height=5

  >>> r.get_size()

  (10, 5)

  >>> r.set_size((150,100))

  #注意:

  #r.set_size((150,100))即:

  #self.width,self.height=(150,100)或150,100 即:

  #self.width=150,self.height=100

  >>> r.width

  150

  >>> r.height

  100

  2.但是这样设置和取得属性值(这里指size的值)太麻烦了,如果要设置和取得多个属性的值,要使用非常多次的set和get方法,所以,这里,我们将set和get方法封装起来,让用户像width和height一样快速赋值和访问。

  >>> class Rectangle:

  ... def __init__(self):

  ... self.width=0

  ... self.height=0

  ... def set_size(self,size):

  ... self.width,self.height=size

  ... def get_size(self):

  ... return self.width,self.height

  #使用property函数,将size的get和set方法都封装到size这个变量中

  ... size=property(get_size,set_size)

  ...

  >>> r=Rectangle()

  >>> r.width=10

  >>> r.height=5

  >>> r.size #快速访问size,取得size的值,无需关心size内部的获取值的函数细节

  (10, 5)

  >>> r.size=150,100

  >>> r.width

  150

  >>> r.size=(100,50) #快速设置size的值,无需关心size内部的设置值的函数细节

  >>> r.width

  100

  >>> r.height

  50

  Tips——关于property的参数问题:

  class property([get[, set[, del[, doc]]]])

  #注:

  # 1.get -- 获取属性值的函数

  # 2.set -- 设置属性值的函数

  # 3.del -- 删除属性值函数

  # 4.doc -- 属性描述信息

  没有传递任何参数的时候,如:size=property(),则创建的特性size将既不可读也不可写。

  只传递一个参数的时候,如:size=property(get_size),则创建的特性size将是只读的。

  传递三个参数,即传递set、get和del。

  传递四个参数,即传递set、get、del和doc(文档字符串,用于描述属性,直接传入信息的内容string即可)。如:

  size = property(get, set, del, "I'm the 'x' property.")

  静态方法和类方法郑州人流多少钱 http://mobile.zyyyzz.com/

  >>> class MyClass:

  ... def meth():

  ... print("This is a common method")

  # 创建静态方法

  # 方法一:手工替换

  ... def smeth():

  ... print("This is a static method")

  ... smeth=staticmethod(smeth)

  # 方法二:使用修饰器

  ... # @staticmethod

  ... # def smeth():

  ... # print("This is a static method")

  # 创建类方法

  # 方法一:手工替换

  ... def cmeth(cls):

  ... print("This is a class method")

  ... cmeth=classmethod(cmeth)

  # 方法二:使用修饰器

  ... # @classmethod

  ... # def cmeth(cls):

  ... # print("This is a class method")

  ...

  #通过类直接访问方法

  >>> MyClass.meth()

  This is a common method

  >>> MyClass.smeth()

  This is a static method

  >>> MyClass.cmeth()

  This is a class method

  #通过类实例访问方法

  >>> myclass=MyClass()

  >>> myclass.meth() #实例myclass会将自身作为一个参数传递给meth方法,而meth方法并没有为它定义参数self,从而导致异常

  Traceback (most recent call last):

  File "", line 1, in

  TypeError: meth() takes 0 positional arguments but 1 was given

  >>> myclass.smeth()

  This is a static method

  >>> myclass.cmeth()

  This is a class method

python基础特性之函数property的更多相关文章

  1. python基础——特性(property)、静态方法(staticmethod)和类方法(classmethod)

    python基础--特性(property) 1 什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 import math class Circl ...

  2. python基础——内置函数

    python基础--内置函数  一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...

  3. python基础——高阶函数

    python基础——高阶函数 高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 以Python内置的求绝对值的函数a ...

  4. python学习第五讲,python基础语法之函数语法,与Import导入模块.

    目录 python学习第五讲,python基础语法之函数语法,与Import导入模块. 一丶函数简介 1.函数语法定义 2.函数的调用 3.函数的文档注释 4.函数的参数 5.函数的形参跟实参 6.函 ...

  5. 自学Python之路-Python基础+模块+面向对象+函数

    自学Python之路-Python基础+模块+面向对象+函数 自学Python之路[第一回]:初识Python    1.1 自学Python1.1-简介    1.2 自学Python1.2-环境的 ...

  6. Python基础(协程函数、内置函数、递归、模块和包)-day05

    写在前面 上课第五天,打卡: 凭着爱,再回首: 一.协程函数(生成器:yield的表达式形式) 1.yield 的语句形式: yield 1 - 这种方式在 Python基础(函数部分)-day04  ...

  7. python基础-内置函数详解

    一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...

  8. 『Python基础-13』函数 Function

    这篇笔记记录的知识点: 函数的基本概念 自定义函数 函数的几种参数 编程的三种方式: 1.OOP 面向对象编程,万物皆对象,以class为主,抽象化 2.POP 面向过程编程,万事皆过程,def定义过 ...

  9. python 基础篇 11 函数进阶----装饰器

    11. 前⽅⾼能-装饰器初识本节主要内容:1. 函数名的运⽤, 第⼀类对象2. 闭包3. 装饰器初识 一:函数名的运用: 函数名是一个变量,但他是一个特殊变量,加上括号可以执行函数. ⼆. 闭包什么是 ...

随机推荐

  1. 【串线篇】SpringMvc数据传出

    /** * SpringMVC除过在方法上传入原生的request和session外还能怎么样把数据带给页面 *  * 四大域: *  pageContext:${pageScope.msg }< ...

  2. 05.线程在睡眠时拥有的监视器资源不会被释放(这里使用重入锁ReentrantLock)

    import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public clas ...

  3. PHP7中异常与错误处理与之前版本对比

    PHP7中异常与错误处理与之前版本对比 先上代码 ECHO PHP_VERSION.PHP_EOL; function add (int $left,int $right){ return $left ...

  4. 【多线程】synchronized 和ReentrantLock

    1. 锁的实现 synchronized 是 JVM 实现的,而 ReentrantLock 是 JDK 实现的. 2. 性能 新版本 Java 对 synchronized 进行了很多优化,例如自旋 ...

  5. Qt事件学习

    一.创建Qt gui应用对应的源码: 点击(此处)折叠或打开 //mylineedit.h #ifndef MYLINEEDIT_H #define MYLINEEDIT_H #include < ...

  6. RCC初始化学习

    一.设置RCC时钟 //#define SYSCLK_HSE #define SYSCLK_FREQ_20MHz //#define SYSCLK_FREQ_36MHz //#define SYSCL ...

  7. textAppearance的属性设置

    android:textAppearance="?android:attr/textAppearanceSmall" android:textAppearance="?a ...

  8. 网页head头部meta和link标签使用大全

    <!-- 声明文档使用的字符编码 --> <meta charset="utf-8"> <!-- 声明文档的兼容模式 --> <meta ...

  9. 国内网络安装ubuntu软件慢的解决方法

    以安装scikit-image为例: pip3 install scikit-image==0.13.0 -i https://pypi.tuna.tsinghua.edu.cn/simple 或者 ...

  10. 几个比较好的IT站和开发库官网

    1.IT技术.项目类网站 (1)首推CodeProject,一个国外的IT网站,官网地址为:http://www.codeproject.com,这个网站为程序开发者提供了很好的代码示例以及讲解,不过 ...