函数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. 备份Oracle 数据库。

    #!/bin/bash# 2018-07-07 oracle database back#filename=`date +%Y%m%d`filename=`date +%Y_%m_%d_%H%M`di ...

  2. egg 连接mysql 在mysql 插入数据

    1.配置mysql exports.mysql = { enable: true, package: 'egg-mysql' }; 'use strict'; module.exports = app ...

  3. Spring 讲解(五)

    Spring 中使用 xml 配置开发和使用注解开发案例 1.Spring 中使用 xml 配置开发案例 接口 public interface UserDao { void add(User use ...

  4. Java中"String.equals()“和"=="的区别

    Do NOT use the `==`` operator to test whether two strings are equal! It only determines whether or n ...

  5. find及其他命令

    Find命令 Find / -type f    :f为普通文件 Find / -name *.txt :查找.txt结尾的 Find / -size  +30M   :找根目录下大于30M的文件 F ...

  6. c++11 中的注意事项

    1. C++11标准中让类的析构函数默认也是noexcept(true)的. 但如果显式地为析构函数指定了noexcept,或者类的基类或成员有noexcept(false)的析构函数,析构函数就不会 ...

  7. php strrev()函数 语法

    php strrev()函数 语法 strrev()怎么用? php strrev()函数用于反转字符串,语法是strrev(string),返回已反转的字符串.大理石构件来图加工 作用:反转字符串 ...

  8. Fidder的使用

    默认的header是类似这样的 User-Agent: Fiddler Host: localhost Content-Length: 34 只需要改成这样的 User-Agent: Fiddler ...

  9. [CSP-S模拟测试]:斯诺(snow)(数学+前缀和+树状数组)

    题目传送门(内部题37) 输入格式 第一行一个整数$n$,表示区间的长度. 第二行一个长度为$n$的只包含$0,1,2$的字符串,表示给出的序列. 输出格式 一行一个整数,表示革命的区间的数量. 样例 ...

  10. C# 语法特性

    C# 2.0 1.泛型(Generics). 2.泛型方法.泛型委托.泛型接口. 3.泛型约束(constraints). 4.部分类(partial). 5.匿名方法. C#3.0/C#3.5 1. ...