在类中定义的函数称为方法,主要有三种:实例方法、类方法、静态方法。

class MyTest():

    # 普通实例函数
def func1(self, arg1, arg2):
pass # 类函数
@classmethod
def func2(cls, arg1, agr2):
pass # 静态函数
@staticmethod
def func3(arg1, agr2):
pass

三种函数的区别:

  实例方法(instancemethod 类方法(classmethod) 静态方法(staticmethod)
定义方式 self作为第一个参数 cls作为第一个参数 无强制参数
参数绑定的对象 类的实例,即用self来传递当前类的实例 类对象,即用cls传递当前类对象
调用方式 只能通过类的实例调用 类对象或类的实例均可调用 类对象或类的实例均可调用
访问类/实例成员   可访问类成员,不可访问实例成员 均不可访问类成员和实例成员

由于classmethod和staticmethod方法可以直接使用类对象调用,因此可在类实例化之前,通过classmethod或staticmethod方法,对类做一定的交互,提供额外的构造实例。

class Date():
'''类成员'''
day = 0
month = 0
year = 0 def __init__(self, year=0, month=0, day=0,):
'''实例成员'''
self.year = year
self.month = month
self.day = day # 类函数
# (PS:由于cls表示类对象,而不是类实例。因此如果我们继承Date类,那么所有的子类也都将拥有trans_date1这个方法。)
@classmethod
def trans_date1(cls, date_of_string):
# print(cls.year) true, @classmethod可访问类成员,无法访问实例成员
year, month, day = map(int, date_of_string.split('-'))
return cls(year, month, day) # 返回类的实例 # 静态函数
@staticmethod
def trans_date2(string_data):
# print(year) false, @staticmethod均不可访问类成员和实例成员
year, month, day = map(int, string_data.split('-'))
return Date(year, month, day) # 返回类的实例 def out_date(self):
print("year: ", self.year)
print("month: ", self.month)
print("day: ", self.day) if __name__ == "__main__":
# date = Date(2020, 8, 25)
'''不需实例化类,直接通过 类名.函数名 调用类方法'''
date1 = Date.trans_date1("2020-08-25")
date1.out_date()
'''不需实例化类,直接通过 类名.函数名 调用静态方法'''
date2 = Date.trans_date2("2020-08-25")
date2.out_date() 运行结果:
year:  2020
month:  8
day:  25
year:  2020
month:  8
day:  25

参考:https://www.cnblogs.com/agnewee/p/5653936.html

python之类方法和静态方法的更多相关文章

  1. Python普通方法、静态方法、类方法

    开始 # -*-coding:utf-8-*- # 普通方法,类方法,静态方法的区别 __metaclass__ = type class Tst: name = 'tst' data = 'this ...

  2. 记录一个 关于 python 普通方法,静态方法和类方法 的介绍。@classmethod @staticmethod

    上班时间 只贴看到最厉害的答案 回头总结 http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod ...

  3. python ——面向对象进阶(反射,双下线方法,静态方法,类方法)

    属性 如果你已经了解Python类中的方法,那么属性就非常简单了,因为Python中的属性其实是普通方法的变种. 哎,其实就是这样,我们看一下当我们想查看税后工资的时候,这其实是一个人的属性,但是它却 ...

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

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

  5. Python进阶----类的结构(公有成员 , 私有成员(私有属性,私有方法),类方法,静态方法,属性) ,isinstance 和issubcalss ,元类(type())

    Python进阶----类的结构(公有成员 , 私有成员(私有属性,私有方法),类方法,静态方法,属性) ,isinstance 和issubcalss ,元类(type()) 一丶类的结构细分    ...

  6. Python笔记_第四篇_高阶编程_实例化方法、静态方法、类方法和属性方法概念的解析。

    1.先叙述静态方法: 我们知道Python调用类的方法的时候都要进行一个实例化的处理.在面向对象中,一把存在静态类,静态方法,动态类.动态方法等乱七八糟的这么一些叫法.其实这些东西看起来抽象,但是很好 ...

  7. Python 属性方法、类方法、静态方法、 特殊属性__doc__ (内建属性)

    总结:和类的关联性讲:属性方法>类方法>静态方法 属性方法@property:仅仅是调用方式不用+括号. 类方法@classmethod:访问不了累的属性变量,只可以访问类变量. 静态方法 ...

  8. Python基础之类方法和静态方法

    小叙一会儿: 通常情况下,在类中定义的所有函数(注意了,这里说的就是所有,跟self啥的没关系,self也只是一个再普通不过 的参数而已)都是对象的绑定方法,对象在调用绑定方法时会自动将自己作为参数传 ...

  9. python类属性和对象属性、类的普通方法和静态方法

    类属性和对象属性的定义 class LearnClass(): #类属性 cls_attr = None def __init__(self,arg): #对象属性 self.obj_attr = a ...

随机推荐

  1. 代码优化与sql优化---未完待续

    万丈高楼平地起,还是得打一个好地基呀 减少对变量对重复计算 //一般这么写 for (int i = ; i < list.size(); i++) {...} //建议修改为: for (in ...

  2. loading动态效果

    html <div class="loadingcontainer" :style="{display:disp}"> <div class= ...

  3. node.js 出现server instance pool was destroyed

    初步判断为MongodbClient断开连接的原因. 之所以断开连接,可能是多个操作间隔时间比较久. 解决方法之一,不要调用close方法.

  4. 掌握提高 Web 应用的性能的方法 之 优化 PHP 和 Laravel

    Laravel 有很多东西.但是快不是其中之一.让我们学习一些优化技巧,以加快运行速度! 自从 Laravel 诞生以来,没有一个 PHP 开发人员不受她的影响.他们是喜欢 Laravel 提供的快速 ...

  5. 2w字 + 40张图带你参透并发编程!

    并发历史 在计算机最早期的时候,没有操作系统,执行程序只需要一种方式,那就是从头到尾依次执行.任何资源都会为这个程序服务,在计算机使用某些资源时,其他资源就会空闲,就会存在 浪费资源 的情况. 这里说 ...

  6. IE9知识点汇总

    1.首先ie9不支持flex布局,只能使用float,要想支持ie低版本,两者要同时使用. 2.input框不支持placeholder属性,只能自己加span标签模拟出来,调整样式. 3.单个css ...

  7. getting session bus failed: //bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.

    今天在调试dbus程序的时候,运行程序出现了getting session bus failed: //bin/dbus-launch terminated abnormally with the f ...

  8. C++ Templates (2.1 类模板Stack的实现 Implementation of Class Template Stack)

    返回完整目录 目录 2.1 类模板Stack的实现 Implementation of Class Template Stack 2.1.1 声明类模板 Declaration of Class Te ...

  9. Redis的数据类型及使用场景

    1.redis 的数据类型 String 字符串 Hash 哈希 List 列表 Set 集合 ZSet(Sorted Set) 有序集合 2.使用场景 2.1 String 用户token 可以用r ...

  10. The Successor Representation: Its Computational Logic and Neural Substrates

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Received May 14, 2018; revised June 28, 2018; accepted July 5, 2018.T ...