函数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. 1.MVC的初步了解

    1.MVC简单介绍 1)原理(如图)如果想更加深入了解原理,可以访问此地址(https://www.cnblogs.com/uicodeintoworld/p/8950877.html) 理解:客户端 ...

  2. 编译自己的jdk(使用openJDK源码编译jdk )

    找到openjdk网站(http://hg.openjdk.java.net/) 选择需要编译的版本,浏览readme文件,有获取源码及编译步骤 CentOS-7-x86_64-DVD-1804.is ...

  3. js浮点解决

    function add(a, b) { var c, d, e; try { c = a.toString().split(".")[1].length; } catch (f) ...

  4. vue 前后端数据交互问题解决

    先在vue项目中配置好路由组件路由 然后写相应组件 2 后端 写接口赔路由 第三  解决跨域问题 处理数据交互 这样前端就拿到了数据

  5. UVa 699 The Falling Leaves (树水题)

    Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...

  6. VC++ 字符串操作学习总结

    vc++中各种字符串(转载) http://www.cnblogs.com/tomin/archive/2008/12/28/1364097.html CString ,BSTR ,LPCTSTR之间 ...

  7. LOAD CSV ERROR: The used command is not allowed with this MySQL version

    要执行的sql 把csvload进db LOAD DATA LOCAL INFILE '/path/datas/temp.csv' INTO TABLE test_table_name FIELDS ...

  8. 85、使用TFLearn实现iris数据集的分类

    ''' Created on 2017年5月21日 @author: weizhen ''' #Tensorflow的另外一个高层封装TFLearn(集成在tf.contrib.learn里)对训练T ...

  9. 'pip' 不是内部或外部命令,也不是可运行的程序或批处理文件。

    解决方法: 本方法适用于已经成功安装python并配置了环境变量. 1.到官网下载pip.py文件 https://pypi.python.org/pypi/pip#downloads 点击下载,解压 ...

  10. Notepad++ 连接 FTP 实现编辑 Linux文件

    下载并安装插件 github 下载 :https://github.com/ashkulz/NppFTP/releases/ 安装过程 将下载后解压的文件夹中的 NppFTP.dll 文件,拷贝到 n ...