python之内置装饰器(property/staticmethod/classmethod)
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)的更多相关文章
- Python之内置装饰器property
# -*- coding: utf-8 -*- # author:baoshan class Student(object): def __init__(self, name): self.name ...
- Python内置装饰器@property
在<Python装饰器(Decorators )>一文中介绍了python装饰器的概念,日常写代码时有一个装饰器很常见,他就是内置的@property. 我们一步步的来接近这个概念. 一个 ...
- python设计模式之内置装饰器使用(四)
前言 python内部有许多内建装饰器,它们都有特别的功能,下面对其归纳一下. 系列文章 python设计模式之单例模式(一) python设计模式之常用创建模式总结(二) python设计模式之装饰 ...
- python基础语法16 面向对象3 组合,封装,访问限制机制,内置装饰器property
组合: 夺命三问: 1.什么是组合? 组合指的是一个对象中,包含另一个或多个对象. 2.为什么要用组合? 减少代码的冗余. 3.如何使用组合? 耦合度: 耦: 莲藕 ---> 藕断丝连 - 耦合 ...
- python内置装饰器
前言 接着上一篇笔记,我们来看看内置装饰器property.staticmethod.classmethod 一.property装饰器 1. 普通方式修改属性值 code class Celsius ...
- python基础之内置装饰器
装饰器 简介 功能与格式 内置装饰器 @classmethod @propertry @staticmethod 其它 ---------------------------------------- ...
- Python 内置装饰器
内置的装饰器 内置的装饰器和普通的装饰器原理是一样的,只不过返回的不是函数,而是类对象,所以更难理解一些. @property 在了解这个装饰器前,你需要知道在不使用装饰器怎么写一个属性. d ...
- 面向对象之classmethod和staticmethod(python内置装饰器)
对象的绑定方法复习classmethodstaticmethod TOC 对象的绑定方法复习 由对象来调用 会将对象当做第一个参数传入 若对象的绑定方法中还有其他参数,会一并传入 classmetho ...
- 三个面向对象相关的装饰器@property@staticmathod@classmethod
@property 先看实例: from math import pi class Circle: def __init__(self,r): self.r = r @property def per ...
随机推荐
- redis在windows系统下的安装和两个问题
今天首次接触redis,遇到一些问题,查了一些资料,在这里汇总整理下. redis的安装看这个:http://www.runoob.com/redis/redis-install.html. 问题1: ...
- Linux LVM扩容和缩容
将原硬盘上的LVM分区/dev/mapper/RHEL-Data由原来的60G扩展到80G Step1:将LVData扩容+20G,如下图: [root@esc data]# lvextend -L ...
- http://ctf.bugku.com/challenges#%E9%80%86%E5%90%91%E5%85%A5%E9%97%A8:bugku--逆向入门
文件是: 分析挺简单,主要是data urls知识点. 首先使用peid检测是否加壳,发现它居然是jpg文件.使用notepad++查看,结果如下. 嗯,百度一下子,知道了data ...
- Springboot学习05-自定义错误页面完整分析
Springboot学习06-自定义错误页面完整分析 前言 接着上一篇博客,继续分析Springboot错误页面问题 正文 1-自定义浏览器错误页面(只要将自己的错误页面放在指定的路径下即可) 1-1 ...
- ES6的let和const的不同点
详细的异同点请点击:https://blog.csdn.net/zhouziyu2011/article/details/71366078
- [leetcode]156.Binary Tree Upside Down颠倒二叉树
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- python的基本用法(四)文件操作使用
#读文件,文件必须存在才能读f=open('操作文件',encoding='utf-8')res =f.read()print(res)f.close()#写文件fw=open('操作文件',mode ...
- 如何在3GPP下载协议
以下载AT Commands协议为例. 1. 打开3GPP网址:http://www.3gpp.org/ 2. 打开Specification-Specification Numbering,如图.去 ...
- Android R文件介绍
R.java 文件中默认有attr.drawable.layout.string等色哥静态内部类,每个静态内部类分别对应着一种资源,如layout静态内部类对应着layout中的接界面文件,其中每个静 ...
- trinitycore 魔兽服务器源码分析(三) 多线程相关
先看LockedQueue.h template <class T, typename StorageType = std::deque<T> >class LockedQue ...