python内置了property、staticmethod、classmethod三个装饰器,有时候我们也会用到,这里简单说明下

1、property

作用:顾名思义把函数装饰成属性

一般我们调用类方法成员,都是如下写法:

class propertyTest():
def __init__(self,x,y):
self.x = x
self.y = y def square(self):
return self.x * self.y pt = propertyTest(3,5)
print(pt.square())

这里一看square就是类的一个方法,但如果把他写成如下形式,那么就不确定调用的一定是类方法:

class propertyTest():
def __init__(self,x,y):
self.x = x
self.y = y @property
def square(self):
return self.x * self.y pt = propertyTest(3,5)
print(pt.square)

这里调用方法类似调用了一个成员变量一样,如果写成print(pt.square())编译器会报错

这就是property的用法, 把一个方法变成一个变量来调用

2、staticmethod

作用:不需要实例化,直接可以调用类中的方法,如下所示

class A():
def __init__(self):
pass @staticmethod
def plus(x,y):
print(x*y) c = A()
c.plus(2,3)
A.plus(4,5)

我们可以实例化类A,然后调用方法plus,也可以直接类.方法调用

3、classmethod

作用:和staticmethod类似,不同的是把调用的类作为第一个参数传入,如下:

class A():
def __init__(self):
pass @classmethod
def plus(cls,x,y):
print(cls)
print(x*y) A.plus(4,5)
c = A()
c.plus(5,6)

这里print(cls)打印的是类A,其他用法同staticmethod

python之内置装饰器(property/staticmethod/classmethod)的更多相关文章

  1. Python之内置装饰器property

    # -*- coding: utf-8 -*- # author:baoshan class Student(object): def __init__(self, name): self.name ...

  2. Python内置装饰器@property

    在<Python装饰器(Decorators )>一文中介绍了python装饰器的概念,日常写代码时有一个装饰器很常见,他就是内置的@property. 我们一步步的来接近这个概念. 一个 ...

  3. python设计模式之内置装饰器使用(四)

    前言 python内部有许多内建装饰器,它们都有特别的功能,下面对其归纳一下. 系列文章 python设计模式之单例模式(一) python设计模式之常用创建模式总结(二) python设计模式之装饰 ...

  4. python基础语法16 面向对象3 组合,封装,访问限制机制,内置装饰器property

    组合: 夺命三问: 1.什么是组合? 组合指的是一个对象中,包含另一个或多个对象. 2.为什么要用组合? 减少代码的冗余. 3.如何使用组合? 耦合度: 耦: 莲藕 ---> 藕断丝连 - 耦合 ...

  5. python内置装饰器

    前言 接着上一篇笔记,我们来看看内置装饰器property.staticmethod.classmethod 一.property装饰器 1. 普通方式修改属性值 code class Celsius ...

  6. python基础之内置装饰器

    装饰器 简介 功能与格式 内置装饰器 @classmethod @propertry @staticmethod 其它 ---------------------------------------- ...

  7. Python 内置装饰器

    内置的装饰器 ​ 内置的装饰器和普通的装饰器原理是一样的,只不过返回的不是函数,而是类对象,所以更难理解一些. @property ​ 在了解这个装饰器前,你需要知道在不使用装饰器怎么写一个属性. d ...

  8. 面向对象之classmethod和staticmethod(python内置装饰器)

    对象的绑定方法复习classmethodstaticmethod TOC 对象的绑定方法复习 由对象来调用 会将对象当做第一个参数传入 若对象的绑定方法中还有其他参数,会一并传入 classmetho ...

  9. 三个面向对象相关的装饰器@property@staticmathod@classmethod

    @property 先看实例: from math import pi class Circle: def __init__(self,r): self.r = r @property def per ...

随机推荐

  1. Python设计模式 - 基础 - 七大基本原则

    提倡使用设计模式,主要出发点就是实现代码复用,增加代码的扩展性和可维护性.如何设计出简洁.易懂.灵活.优美的代码结构的确是一门学问,透彻理解并践行如下七大原则通常都能取得基本满意的结果: - 单一职责 ...

  2. 移动端的1px边框问题

    最近在做一个移动端项目,涉及到1像素问题 其实质就是移动端的css里写1px,看起来比1px粗,这就是物理像素和逻辑像素的区别.物理像素和逻辑像素之间存在一个比例关系,在Javascript中可以用w ...

  3. yum安装命令:遇到的问题报错如下: File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: 通过看报错可以了解到是使用了python2的语法,所以了解到当前yum使用的Python2,因为我单独安装了python3,且python3设置为默认版本了,所以导致语法问题 解决方法: 使用python2.6 yum install

    1.安装zip yum install -y unzip zip 2.安装lrszs yum -y install lrzsz 3.安装scp 遇到下面的问题: 结果提示: No package sc ...

  4. 100-days: sixteen

    Title: The world's most expensive cities 生活成本最高的城市 For the first time in its 30-year history, the Wo ...

  5. 516. Longest Palindromic Subsequence最长的不连续回文串的长度

    [抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...

  6. 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表

    [抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...

  7. ramfs的两种制作方法

    制作方法1 1  准备一个已经可以使用的文件系统,假设目录为/rootfsLinux内核需要支持ext2文件系统及ramdisk支持(fs相应的选项要勾上)2 在pc上制作ramdisk镜像(1)dd ...

  8. mysql开启调试日志general_log开启跟踪日志

    general_log = 1 general_log_file = /tmp/umail_mysql.log 有时候,不清楚程序执行了什么sql语句,但是又要排除错误,找不到原因的情况下, 可以在m ...

  9. MySQL skip-character-set-client-handshake导致的一个字符集问题

    http://www.quweiji.com/mysql-skip-character-set-client-handshake%E5%AF%BC%E8%87%B4%E7%9A%84%E4%B8%80 ...

  10. 归并排序(递归排序and外排排序)

    分析: /** * 归并排序 (先将数组利用归并排序排成 有序的左边数组和右边数组,再比较左边数组和右边数组的数值大小进行排序) * */ public class MergeSort { publi ...