python类方法@classmethod与@staticmethod

一、@classmethod

介绍

与普通的类方法有所不同的是,用@classmethod修饰的类方法不传入self实例本身,而是传入cls,代表这个类自身,可以来调用类的属性,类的方法,实例化对象等。

语法

使用的语法也非常简单,直接在类方法上加上装饰器@classmethod即可,另外传入cls参数作为方法的第一个参数。

class A(object):

    @classmethod
def func(cls):
pass

举例

class A(object):
num = 1 def func1(self):
print('func1') @classmethod
def func2(cls):
print('func2')
print(cls.num)
cls().func1() if __name__ == '__main__':
A.func2() ------------------------------
>>> func2
>>> 1
>>> func1

二、@staticmethod

介绍

使用@staticmethod修饰的类方法也被称为静态方法,此方法不传入代表实例对象的self参数,并且不强制要求传递任何参数,可以被类直接调用,当然实例化的对象也可以调用。

语法

使用时直接在类方法上加上装饰器@staticmethod,参数不需要self,其他参数也是可选。

class B(object):

    @staticmethod
def func()
pass

举例

class B(object):

    @staticmethod
def func1():
print('func1') @staticmethod
def func2(a, b):
print('func2')
print('a=', a)
print('b=', b) if __name__ == '__main__':
B.func1()
b = B()
b.func1()
B.func2(1, 2) ------------------------------
>>> func1
>>> func1
>>> func2
>>> a= 1
>>> b= 2

python类方法@classmethod与@staticmethod的更多相关文章

  1. python的@classmethod和@staticmethod

    本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象 ...

  2. Python的classmethod和staticmethod区别

    静态方法(staticmethod) 类方法(classmethod) 静态方法和类方法都可以通过类名.方法名或者实例.方法访问. #-*- coding:utf8 -*- class A(objec ...

  3. Python中classmethod与staticmethod区别

    classmethod:类方法staticmethod:静态方法 在python中,静态方法和类方法都是可以通过类对象和类对象实例访问.但是区别是: @classmethod 是一个函数修饰符,它表示 ...

  4. 粗解python的@classmethod和@staticmethod及普通实例方法

    引言: 使用不同的函数定义方法,可以使得函数定义更加有效而且易于维护 本文为博主原创,根据本人自己的理解整理而成,若有不准确的地方,希望能留言告知以免误导他人: 首先进一段代码,来直观感受一下不同类型 ...

  5. Python中classmethod和staticmethod的区别

    学习python中经常会出现一些相近或者相似的语法模块等,需要对比分析才能加深记忆,熟练运用. staticmethod:静态方法 classmethod:类方法 在python中,静态方法和类方法都 ...

  6. 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别

    [面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...

  7. Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法

    Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  8. Python - 静态方法@staticmethod和类方法classmethod

    传送门 https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/Day09/%E9%9D%A2%E5%90%91%E5%AF ...

  9. Classmethod and Staticmethod - Python 类方法 和 静态方法

    classmethod and staticmethod classmethod 的是一个参数是类对象 cls (本类,或者子类), 而不是实例对象 instance (普通方法). classmet ...

随机推荐

  1. Spring Boot (三): ORM 框架 JPA 与连接池 Hikari

    前面两篇文章我们介绍了如何快速创建一个 Spring Boot 工程<Spring Boot(一):快速开始>和在 Spring Boot 中如何使用模版引擎 Thymeleaf 渲染一个 ...

  2. elasticsearch集群扩容和容灾

    elasticsearch专栏:https://www.cnblogs.com/hello-shf/category/1550315.html 一.集群健康 Elasticsearch 的集群监控信息 ...

  3. at org.apache.jsp.WEB_002dINF.pages.login_jsp._jspInit( login_jsp.java:22)

    SEVERE: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException at org.apac ...

  4. 一步步构建.NET Core Web应用程序---仓储层,业务层的实现

    前言 上一篇文章介绍了整个项目的结构,接下来向大家介绍一下 我的 仓储及业务层具体的实现思路,如果有更好的实现方式,希望大家及时指出!!! 构建过程 一,数据访问 首先在 DataProvider 中 ...

  5. wait()与notify()

    一,前言 ​ ​ 简单画了一下线程的流程图,只是一个大概.如图所示,线程有多种状态,那么不同状态之间是如何切换的,下面主要总结关于wait()和notify()的使用. 二,wait() ​ wait ...

  6. 如何在女友卸妆后,正确的找到她?---java中使用反射的小秘密

    故事背景 小白是个程序猿,刚毕业两年,最近交了一个女朋友,是同事介绍的.女朋友和闺蜜住在一起.小白早上很早接到女朋友电话,昨天她的一个文件错放到了他的电脑包,希望他帮忙送到她住的地方,她今天要向她bo ...

  7. C语言入门-函数

    一.初见函数 求出1到10.20到30和35到45的三个的和 #include <stdio.h> // 定义一个函数 void sum(int begin, int end) { int ...

  8. 使用apache的poi来实现数据导出到excel的功能——方式二

    此次,介绍利用poi与layui table结合导出excel.这次不需要从数据库中查询出来的数据进行每一行的拼接那么麻烦,我们这次将标题定义一个id值,对应从数据库中查找出来的字段名即可. 1.po ...

  9. nginx 配置语法

    一.语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url做编码 ...

  10. 一起来学Java注解(Annotation)

    目录 一. 什么是Annotation 二. Annotation的作用 2.1 编译器使用到的注解 2.2 .class文件使用到的注解 2.3 运行期读取的注解 三. 定义Annotation 3 ...