python的类方法和静态方法
作者:shichen2014 时间:2021-04-14 21:05:22
本文实例讲述了python的类方法和静态方法。分享给大家供大家参考。具体分析如下:
python没有和C++中static关键字,它的静态方法是怎样的呢?还有其它语言中少有的类方法又是神马?
python中实现静态方法和类方法都是依赖于python的修饰器来实现的。
class MyClass:
def method(self):
print("method")
@staticmethod
def staticMethod():
print("static method")
@classmethod
def classMethod(cls):
print("class method")
大家注意到普通的对象方法、类方法和静态方法的去别了吗?
对象方法有self参数,类方法有cls参数,静态方法是不需要这些附加参数的。
在C++中是没有类方法着个概念的的
class A(object):
"This ia A Class"
@staticmethod
def Foo1():
print("Call static method foo1()\n")
@classmethod
def Foo2(cls):
print("Call class method foo2()")
print("cls.__name__ is ",cls.__name__)
A.Foo1();
A.Foo2();
结果是:
Call static method foo1()
Call class method foo2()
cls.__name__ is A
希望本文所述对大家的Python程序设计有所帮助。
标签:python,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
下载糗事百科的内容_python版
2022-02-08 12:39:29
SQL Transcation的一些总结分享
2012-08-21 10:21:28
使用python 获取进程pid号的方法
2023-06-11 21:25:52
Javascript函数类型判断解决方案
2009-08-27 15:32:00
django中使用原生sql语句的方法步骤
2023-06-29 16:36:07
WEB前端开发经验总结
2009-07-20 18:01:00
![](https://img.aspxhome.com/file/UploadPic/20097/20/standard-1-62s.jpg)
关于python并发编程中的协程
2023-10-18 04:37:44
使用Python webdriver图书馆抢座自动预约的正确方法
2021-01-26 10:21:07
![](https://img.aspxhome.com/file/2023/9/92439_0s.jpg)
Pyecharts绘制全球流向图的示例代码
2021-04-30 14:52:56
![](https://img.aspxhome.com/file/2023/5/103515_0s.png)
Python中字符串List按照长度排序
2023-11-28 21:43:02
SQL语句参考及记录集对象详解
2008-11-25 11:47:00
Python 矩阵转置的几种方法小结
2023-01-20 15:51:56
深入了解Python在HDA中的应用
2023-08-02 06:59:47
![](https://img.aspxhome.com/file/2023/6/86426_0s.jpg)
python解决js文件utf-8编码乱码问题(推荐)
2023-03-19 04:55:27
深入理解Python爬虫代理池服务
2022-11-05 17:20:29
![](https://img.aspxhome.com/file/2023/9/100819_0s.png)
举例讲解Python面向对象编程中类的继承
2022-02-09 02:59:14
Python的Django框架中的URL配置与松耦合
2022-11-19 10:23:33
Python入门教程3. 列表基本操作【定义、运算、常用函数】 <font color=red>原创</font>
2023-07-15 13:09:19
利用Python实现自动扫雷小脚本
2023-05-10 01:45:27
![](https://img.aspxhome.com/file/2023/7/84977_0s.jpg)
canvas 2d 环形统计图手写实现示例
2023-07-13 16:35:23
![](https://img.aspxhome.com/file/2023/1/55911_0s.jpg)