@staticmethod和classmethod
之前一直搞不清楚这两个类方法有什么区别,今天着重学习了一下
@staticmethod是静态方法,不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
class C(object):
@staticmethod
def f():
print('runoob'); C.f(); # 静态方法无需实例化
cobj = C()
cobj.f() # 也可以实例化后调用
classmethod是类方法,对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。
class A(object):
bar = 1
def func1(self):
print ('foo')
@classmethod
def func2(cls):
print ('func2')
print (cls.bar)
cls().func1() # 调用 foo 方法 A.func2() # 不需要实例化
再举一个例子,如果一个类定义了日期,需要输出年月日,然而有时候输入的格式是“年-月-日”的形式,又需要先把这种格式转化为年月日,再输出,这时候就需要一个方法去改变类的输入值,而不是去改变init初始化函数,这个方法就是类方法或静态方法,详见如下
class Data_test2(object):
day=0
month=0
year=0
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year @staticmethod ###静态方法
def get_date(data_as_string):
year,month,day=map(int,data_as_string.split('-'))
date1=Data_test2(year,month,day)
#返回的是一个初始化后的类
return date1 def out_date(self):
print("year :")
print(self.year)
print("month :")
print(self.month)
print("day :")
print(self.day)
r=Data_test2.get_date("2016-8-6")
r.out_date()
class Data_test2(object):
day=0
month=0
year=0
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year @classmethod ###类方法
def get_date(cla, data_as_string):
#这里第一个参数是cls, 表示调用当前的类名
year,month,day=map(int,data_as_string.split('-'))
date1=cls(year,month,day)
#返回的是一个初始化后的类
return date1 def out_date(self):
print("year :")
print(self.year)
print("month :")
print(self.month)
print("day :")
print(self.day)
r=Data_test2.get_date("2016-8-6")
r.out_date()
@staticmethod和classmethod的更多相关文章
- Python语言特性之3:@staticmethod和@classmethod
问题:Python中@staticmethod和@classmethod两种装饰器装饰的函数有什么不同? 原地址:http://stackoverflow.com/questions/136097/w ...
- @staticmethod和@classmethod
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...
- python(三)@staticmethod和@classmethod使用和区别
转载自[1] 一般要用某个类的方法,先要实例这个类. 但是可以通过@staticmethod和@classmethod,直接用“类.方法()”来调用这个方法. 而 @staticmethod和@cla ...
- python @staticmethod和@classmethod的作用
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...
- 手动实现staticmethod和classmethod装饰器
首先,staticmethod和classmethod装饰器是通过非数据描述符实现的.用法简单,这里就不细说了. 这里主要分析一下staticmethod和classmethod是如何通过描述符实现的 ...
- (转)关于python3中staticmethod(静态方法)classmethod(类方法)实例方法的联系和区别
原文:http://dmcoders.com/2017/08/30/pythonclass/ https://zhuanlan.zhihu.com/p/28010894------正确理解Python ...
- python中@staticmethod与@classmethod
@ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...
- python 之@staticmethod和@classmethod
在python中,要调用一个类中的方法,一般的操作步骤如下: 1.实例化此类 2.调用此类中的方法 而@staticmethod和@classmethod则打破了这种引用方式,可以在不实例化类的情况下 ...
- python中 staticmethod与classmethod区别
staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...
- @staticmethod和@classmethod的作用与区别
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...
随机推荐
- hdu 3555 Bomb ( 数位DP)
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...
- Linux 添加开机启动项的两种方法
1.编辑文件 /etc/rc.localvim /etc/rc.local #!/bin/sh## This script will be executed *after* all the oth ...
- 【Linux】关于ffmpeg的一些常见用法
一.FFmpeg简介 FFmpeg是一款非常快速的视频和音频转换器, 是开源项目 FFmpeg (Fast Forward moving pictures expert group) 的命令行程序. ...
- ZMQ示例:使用 curve 进行加密通信
1. ZMQ 官方文档 ZMQ 的官方文档中关于 curve 的介绍如下: Client and server roles A socket using CURVE can be either cli ...
- C# 批量图片打包下载
实现点击下载功能,可以一次性下载多个附件.具体实现代码如下 : private void Com_XZTP_FJ(string maiId, string wtfjdz, string CLwtfjd ...
- 用.NET开发的磁力搜索引擎——btbook.net
UPDATE:目前项目已停止维护,本文仅留作纪念. 去年10月份开始研究相关的协议与资料,中途乱七八糟的事情差点没坚持下来,寒假里修修补补上礼拜把Btbook发布了,经过社交网络的推广之后,上线第三天 ...
- Cocos Lua的Touch 点击事件添加
两种方式: -- 触摸开始 local function onTouchBegan(touch, event) return true end -- 触摸结束 local function onTou ...
- MUI框架的缩写输入
html 上面--代表最小触发字符 下面--代表非必要完整触发字符 组件 触发字符 mDoctype(mui-dom结构) mdo ctype mBody(主体) mbo dy m ...
- vscode + gradle 创建 java 项目 - java language server无法启动
1.在系统上安装一个版本的gradle,用`gradle init --type java-application`创建一个默认的java项目,假设项目目录是hellojava 2.vscode写ja ...
- bash python获取文本中每个字符出现的次数
bash: grep -o . myfile | sort |uniq -c python: 使用collections模块 import pprint import collections f = ...