函数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. egg 的学习

    1.初始化项目 快速生成项目: $ npm i egg-init -g $ egg-init egg-example --type=simple $ cd egg-example $ npm i 启动 ...

  2. 目标检测中roi的有关操作

    1.roi pooling 将从rpn中得到的不同Proposal大小变为fixed_length output, 也就是将roi区域的卷积特征拆分成为H*W个网格,对每个网格进行maxpooling ...

  3. mysql查询表的创建时间

    mysql查询表的创建时间 查询语句: SELECT table_name,create_time FROM information_schema.TABLES;

  4. 【持久层】Druid简介

    Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBoss DataSou ...

  5. 理解同步/异步/阻塞/非阻塞IO区别

    5种IO模型 1.阻塞式I/O模型 阻塞I/O(blocking I/O)模型,进程调用recvfrom,其系统调用直到数据报到达且被拷贝到应用进程的缓冲区中或者发生错误才返回.进程从调用recvfr ...

  6. H5 调用 手机设备的功能

    1.调用 邮件 : 参考 https://blog.csdn.net/github_38516987/article/details/77637546 (亲测有效) <a href=" ...

  7. php中的list()

    list()在php中上一个语言结构,并不是一个函数.类似array(),不过array()这个东西我们现在一般很少使用了,因为从php5.4版本开始,我们会直接使用[]来定义数组. 那么,list( ...

  8. 购买一台阿里云云主机(CentOS)后

    系统的优化优化之前,首先查看版本信息 cat /etc/redhat-release CentOS release 6.9 (Final) 查看内核版本 uname -a Linux iZwz98ak ...

  9. tarjam 模板改编

    思路要灵活 邻接表涉及数组问题,可以用vector代替

  10. Apache和Tomcat的区别是什么?

    Apache 和 Tomcat 都是web网络服务器,两者既有联系又有区别,在进行HTML.PHP.JSP.Perl等开发过程中,需要准确掌握其各自特点,选择最佳的服务器配置. Apache是web服 ...