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 ...
随机推荐
- 二十、Flyweight 享元模式
原理: 代码清单: BigChar public class BigChar { //字符名称 private char charname; //大型字符 # . \n 组成 private Stri ...
- [Solution] 950. Reveal Cards In Increasing Order
Difficulty: Medium Problem In a deck of cards, every card has a unique integer. You can order the de ...
- django 请求与响应
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SD
Offer(Tcode:VA23;Table: vbak and vbap) billing(Tcode:VF03;Table:vbrk and vbrp) Offer(quotation)-> ...
- Error while executing: am start -n
单纯clean project是不行的,选择File-Invalidate Caches/Restart,清除缓存,重启AS Android NDK 是在SDK前面又加上了"原生" ...
- Arrays工具类和Collections工具类
集合知识点总结 Arrays工具类 .binarySearch() .sort() .fill() //填充 int[] array = new int[10]; Arrays.fill(array, ...
- USART of STM32
/*************************************************************************** * 文件名:USART.h * * 编写人:离 ...
- Scala数组小结
1.定长数组 定长数组:指长度不可变的数组Array. 第一种方式: 先声明一个数组,后初始化该数组: scala> val array = new Array[Double](5) array ...
- Swagger相关配置记录
1.SwaggerConfig文件配置 public class SwaggerConfig { protected static string GetXmlCommentsPath() { retu ...
- 【论文学习】YOLO9000: Better,Faster,Stronger(YOLO9000:更好,更快,更强)
原文下载:https://arxiv.org/pdf/1612.08242v1.pdf 工程代码:http://pjreddie.com/darknet/yolo/ 目录 目录 摘要 简介 BETTE ...