python类方法@classmethod与@staticmethod
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的更多相关文章
- python的@classmethod和@staticmethod
本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象 ...
- Python的classmethod和staticmethod区别
静态方法(staticmethod) 类方法(classmethod) 静态方法和类方法都可以通过类名.方法名或者实例.方法访问. #-*- coding:utf8 -*- class A(objec ...
- Python中classmethod与staticmethod区别
classmethod:类方法staticmethod:静态方法 在python中,静态方法和类方法都是可以通过类对象和类对象实例访问.但是区别是: @classmethod 是一个函数修饰符,它表示 ...
- 粗解python的@classmethod和@staticmethod及普通实例方法
引言: 使用不同的函数定义方法,可以使得函数定义更加有效而且易于维护 本文为博主原创,根据本人自己的理解整理而成,若有不准确的地方,希望能留言告知以免误导他人: 首先进一段代码,来直观感受一下不同类型 ...
- Python中classmethod和staticmethod的区别
学习python中经常会出现一些相近或者相似的语法模块等,需要对比分析才能加深记忆,熟练运用. staticmethod:静态方法 classmethod:类方法 在python中,静态方法和类方法都 ...
- 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别
[面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...
- Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法
Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- Python - 静态方法@staticmethod和类方法classmethod
传送门 https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/Day09/%E9%9D%A2%E5%90%91%E5%AF ...
- Classmethod and Staticmethod - Python 类方法 和 静态方法
classmethod and staticmethod classmethod 的是一个参数是类对象 cls (本类,或者子类), 而不是实例对象 instance (普通方法). classmet ...
随机推荐
- 如何使用rsync备份
已知3台服务器主机名分别为web01.backup .nfs主机信息见下表: 角色 外网IP(NAT) 内网IP(LAN) 主机名 WEB eth0:10.0.0.7 eth1:172.16.1.7 ...
- 52 (OC)* 苹果手机各种尺寸详细表以及iPhoneX、iPhoneXS、iPhoneXR、iPhoneXSMax屏幕适配
iPhone设备 物理分辨率是硬件所支持的,逻辑分辨率是软件可以达到的. 代数 设备 操作系统 逻辑分辨率(point) 物理分辨率(pixel) 屏幕尺寸(对角线长度) 缩放因子 iPhone ...
- PiVot 用法
基本语法: SELECT <非透视的列>, [第一个透视的列] AS <列名称>, [第二个透视的列] AS <列名称>, ... [最后一个透视的列] AS &l ...
- Tcloud 云测平台-多服务框架开源
技术栈 Python3.7 + Vue前端github地址:https://github.com/bigbaser/Tcloud后端github地址:https://github.com/bigbas ...
- 文章导航-readme
Spring-Boot Spring Boot(一) Hello World Redis 图解Redis之数据结构篇--简单动态字符串SDS 图解Redis之数据结构篇--链表 图解Redis之数据结 ...
- 在Docker中启动Cloudera
写在前面 记录一下,一个简单的cloudera处理平台的构建过程和一些基本组件的使用 前置说明 需要一台安装有Docker的机器 docker常用命令: docker ps docker ps -a ...
- 看完您如果还不明白 Kerberos 原理,算我输!
系统环境 操作系统:CentOS 6 或 CentOS 7 JDK 版本:1.8.0_151 Ambari 版本:2.6.1 HDP 版本:2.6.4.0 扩展链接 Kerberos原理--经典对话 ...
- mysql root密码忘记
首先停掉mysql服务,在/etc/my.cnf中添加 skip-grant-tables,同时可以添加skip-networking选项来禁用网络功能,防止这时其他人通过网络连接到数据库 [mysq ...
- Android self_adaption of screen
以下是Demo首页的预览图 demo下载:http://www.eoeandroid.com/forum.php?mod=attachment&aid=NjE0Njh8ZTIyZDA2M2N8 ...
- Future类型的连锁的运用
Future类型的连锁的运用: 洗脸 刷牙 吃早饭 看电视 出门 串行: import scala.concurrent.{Await, Future} import scala.util.{Fail ...