@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,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...
随机推荐
- linux clamav 免费查毒工具
linux下需要杀毒工具吗?我一直认为是不要的,基于linux的病毒很少,linux 安全防护也做的很好,一般很难功破.我想那些黑客们更喜欢,写windows下的病毒,用的人多啊,传播也容易.下面的操 ...
- dotnet ef
dotnet ef migrations add <Name-of-Migration> dotnet ef database update
- ionic 状态栏显示异常 statusBar
从主分支上新建一个分支开发另一个app, 生成之后手机上显示状态栏异常, 如下图, 只显示了电池的色块, 百思不得其解啊. 各种猜测无果, 对比config.xml, 发现statusBar插件版本不 ...
- 【推荐】Hutool 的通用工具类库
摘自3.1.1版本作者发布原话,当时看到有点说不上的情绪,为作者的坚持.热爱点个赞. 已经想不起来是怎样结识 Hutool 的,但 Hutool 伴随几个项目的推进,获得了同事一致好评. 没经过实践和 ...
- java 根据系统日期获取前一天、后一天时间(根据初始日期推算出期望(向前/向后)日期)
1.情景展示 java 根据系统当前日期获取前一天日期.后一天日期,或者根据初始日期推算出期望(向前/向后)日期. 2.解决方案 导包 import java.text.ParseExcepti ...
- PHP异步扩展Swoole笔记(1)
安装Swoole扩展 通过pecl安装, 系统中最好已经有http2依赖, 如果是Ubuntu, 可以直接通过apt安装nghttp2, 如果是Centos或者需要自己编译, 在Github下载ngh ...
- Nload(CentOS网速的实时监控)
Nload(CentOS网速的实时监控)的安装和安装过程中的问题 I. 安装 Download the latest rpmforge-release rpm from wget ftp://ftp. ...
- 认知:关于Android 调试的坑
要注意充电线和数据线的区别! 要注意充电线和数据线的区别! 要注意充电线和数据线的区别! 可以通过访问 :chrome://inspect/#devices 查看设备是否正常. 通常电脑也会有提示, ...
- k8s yaml说明
k8s yaml # yaml格式的pod定义文件完整内容: apiVersion: v1 #必选,版本号,例如v1 kind: Pod #必选,Pod metadata: ...
- Python3将ipa包中的文件按大小排序
[本文出自天外归云的博客园] 给你个ipa包,解压前输出包大小,解压后把里面的文件按大小排序.代码如下: import os import shutil import zipfile _ipa_zip ...