【python】classmethod & staticmethod 区别
来源:http://blog.csdn.net/carolzhang8406/article/details/6856817
其他参考:
http://blog.csdn.net/lovingprince/article/details/6595466
http://blog.csdn.net/handsomekang/article/details/9615239
http://python.jobbole.com/83584/
比较好的讨论:
http://bbs.csdn.net/topics/350141376
https://www.zhihu.com/question/31844003
classmethod:类方法
staticmethod:静态方法
在python中,静态方法和类方法都是可以通过类对象和类对象实例访问。但是区别是:
- @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法。 类方法的第一个参数cls,而实例方法的第一个参数是self,表示该类的一个实例。
- 普通对象方法至少需要一个self参数,代表类对象实例
- 类方法有类变量cls传入,从而可以用cls做一些相关的处理。并且有子类继承时,调用该类方法时,传入的类变量cls是子类,而非父类。 对于类方法,可以通过类来调用,就像C.f(),有点类似C++中的静态方法, 也可以通过类的一个实例来调用,就像C().f(),这里C(),写成这样之后它就是类的一个实例了。
- 静态方法则没有,它基本上跟一个全局函数相同,一般来说用的很少
Example 1:
>>> class a():
@staticmethod
def staticm():
print 'static'
def normalm(self):
print 'nomarl',self
@classmethod
def classm(cls):
print 'class',cls
>>> a1=a()
>>> a1.normalm()
nomarl <__main__.a instance at 0x84dddec>
>>> a1.staticm()
static
>>> a1.classm()
class __main__.a
>>> type(a)
<type 'classobj'>
>>> type(a1)
<type 'instance'>
Example 2:
class A(object):
@classmethod
def cm(cls):
print '类方法cm(cls)调用者:', cls.__name__
@staticmethod
def sm():
print '静态方法sm()被调用'
class B(A):
pass
A.cm()
B.cm()
A.sm()
B.sm()
输出:
类方法cm(cls)调用者: A
类方法cm(cls)调用者: B
静态方法sm()被调用
静态方法sm()被调用
#coding=utf-8
class Test(object):
def InstanceFun(self):
print("InstanceFun");
print(self);
@classmethod
def ClassFun(cls):
print("ClassFun");
print(cls);
@staticmethod
def StaticFun():
print("StaticFun");
def Ballshurt(): #该函数怎么调用都是错的
print("Ballshurt");
t = Test();
t.InstanceFun();
print "---------"
Test.ClassFun();
print "---------"
Test.StaticFun();
print "---------"
t.StaticFun();
print "---------"
t.ClassFun();
print "---------"
Test.InstanceFun(t);
print "---------"
Test.InstanceFun(Test);
print "---------"
#Test.Ballshurt(); #error
#t.Ballshurt(); #access error
看上面例子,当类中的函数不添加参数时是错误的,没有办法通过实例或类本身调用,而@staticmethod修饰后就可以访问了。
一些网上观点:
实例方法隐含的参数为类实例,而类方法隐含的参数为类本身。
静态方法无隐含参数,主要为了类实例也可以直接调用静态方法。
所以逻辑上类方法应当只被类调用,实例方法实例调用,静态方法两者都能调用。
注意啊只是逻辑上啊,其实python的类模型实现上灵活而蛋疼着。
这些玩意基本都是为了隐含参数而搞出来的鬼名堂。
了解这点只要参数个数匹配,python就允许你"胡扯"了。
----------------------------------------
Python的类就是个语法糖。一个函数写在类里面和写在类外面没有区别,唯一的区别就是参数,所谓实例方法就是第一个参数是self,所谓类方法就是第一个参数是class,而静态方法不需要额外的参数,所以必须区分。
-----------------------------------------
c++的static方法则是正儿八经的静态方法,因为类定义本身不是个实例。
python里,除了极少的几个语句,其余全都是对象实例。一个函数同样是对象实例,因此对python而言其实连方法这一说都不准确…那些个对象方法只不过是指向函数对象的引用,只是属性而已……
【python】classmethod & staticmethod 区别的更多相关文章
- python -- @classmethod @staticmethod区别和使用
python中的定义: class MyClass: ... @classmethod # classmethod的修饰符 def class_method(cls, arg1, arg2, ... ...
- python中@classmethod @staticmethod区别
Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...
- python中@classmethod @staticmethod区别(转)
pthon中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): print ...
- [转]python中@classmethod @staticmethod区别
Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...
- django classonlymethod 和 python classmethod的区别
--classmethod可以被一个实例调用,classonlyethod只能被类调用 class Kls(object): no_inst = 0 def __init__(self): Kls.n ...
- 基于python中staticmethod和classmethod的区别(详解)
例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object): def foo(self,x): print "executing foo ...
- python @classmethod和@staticmethod区别
python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...
- 模块复习 staticmethod和classmethod的区别
Python中classmethod与staticmethod区别 classmethod:类方法staticmethod:静态方法 在python中,静态方法和类方法都是可以通过类对象和类对象实例访 ...
- python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?
回答背景知识 这些都是装饰器(decorator).装饰器是一种特殊的函数,要么接受函数作为输入参数,并返回一个函数,要么接受一个类作为输入参数,并返回一个类. @标记是语法糖(syntactic s ...
随机推荐
- 国内SEO如何过滤掉不良网络信息
对于站长们来说,首要任务就是和搜索引擎战斗,面对搜索引擎算法的不断更新,站长们也更加头疼.站长们都觉得,搜索引擎才是网站优化的"统治者",和谷歌优化相比,中国的SEO优化要复杂的多 ...
- hibernate中get,load,list,iterate的用法及比较
首先,get和load都是查询单个对象,而list和iterate为批量查询 注意以下写法仅针对hibernate3的语法. 使用案例如下: // 1. get和load 的用法 Person p = ...
- JS获取/设置iframe内对象元素、文档的几种方法
1.IE专用(通过frames索引形象定位): document.frames[i].document.getElementById('元素的ID'); 2.IE专用(通过iframe名称形象定位): ...
- Apache常用2种工作模式prefork和worker比较
Apache两种常用工作模式:prefork和worker. prefork MPM prefork是一个非线程型的.预派生的MPM,使用多个进程,每个进程在某个确定的时间只单独处理一个连接,效率高, ...
- Mac Pro 安装 cmake,报错 Warning: cmake-3.5.2 already installed, it's just not linked
1.先安装 brew,参考文章:Mac Pro 安装 Homebrew 软件包管理工具 2.执行安装命令 brew install cmake 出现警告提示: Warning: cmake-3.5.2 ...
- Swift2.1 语法指南——错误处理
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- "A transport-level error has occurred when sending the request to the server,指定的网络名不在可用"的解决办法
项目在外网服务器上运行的时候,遇到一个异常:"A transport-level error has occurred when sending the request to the ser ...
- Android文本读写
//写文件操作 public void writeFileData(String fileName, String message){ try{ FileOut ...
- js ajax简单使用
文章部分资源来源:(http://blog.csdn.net/lzkkevin/article/details/6777474)以及(http://www.cnblogs.com/hwx0807/ar ...
- POJ 1273 网络流(最大流)模板
http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...