python中staticmethod classmethod及普通函数的区别
staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象
(python里光说对象总是容易产生混淆, 因为什么都是对象,包括类,而实际上
类实例对象才是对应静态语言中所谓对象的东西)来调用而已, 不会隐式地传入
任何参数。这个和静态语言中的静态方法比较像。
classmethod 是和一个class相关的方法,可以通过类或类实例调用,
并将该class对象(不是class的实例对象)隐式地 当作第一个参数传入。
就这种方法可能会比较奇怪一点,不过只要你搞清楚了python里class也是个真实地
存在于内存中的对象,而不是静态语言中只存在于编译期间的类型。
正常的方法 就是和一个类的实例对象相关的方法,通过类实例对象进行调用,
并将该实例对象隐式地作为第一个参数传入,这个也和其它语言比较像。
可如下示例:
#!/usr/bin/python
2.#coding:utf-8
3.
4.#author: gavingeng
5.#date: 2011-12-03 10:50:01
6.
7.class Person:
8.
9. def __init__(self):
10. print "init"
11.
12. @staticmethod
13. def sayHello(hello):
14. if not hello:
15. hello='hello'
16. print "i will sya %s" %hello
17.
18.
19. @classmethod
20. def introduce(clazz,hello):
21. clazz.sayHello(hello)
22. print "from introduce method"
23.
24. def hello(self,hello):
25. self.sayHello(hello)
26. print "from hello method"
27.
28.
29.def main():
30. Person.sayHello("haha")
31. Person.introduce("hello world!")
32. #Person.hello("self.hello") #TypeError: unbound method hello() must be called with Person instance as first argument (got str instance instead)
33.
34. print "*" * 20
35. p = Person()
36. p.sayHello("haha")
37. p.introduce("hello world!")
38. p.hello("self.hello")
39.
40.if __name__=='__main__':
41. main()
output:
i will sya haha
2.i will sya hello world!
3.from introduce method
4.********************
5.init
6.i will sya haha
7.i will sya hello world!
8.from introduce method
9.i will sya self.hello
10.from hello method
python中staticmethod classmethod及普通函数的区别的更多相关文章
- python中的str和repr函数的区别
看了一些网上的解释,最主流的解释是“str是给人看的,repr是给机器看的”,如果已经理解了的,这句话是对的,但是是有问题的,对于没懂的,这句话是无法理解的. 我来尝试解释一下.先直译一下官方文档: ...
- 基于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】Python 中的 classmethod 和 staticmethod
Python 中的 classmethod 和 staticmethod 有什么具体用途? 推荐地址:http://www.cnblogs.com/wangyongsong/p/6750454.htm ...
- Python中的__init__()和__call__()函数
Python中的__init__()和__call__()函数 在Python的class中有一些函数往往具有特殊的意义.__init__()和__call__()就是class很有用的两类特殊的函数 ...
- 关于Python中的classmethod
Python 中的 classmethod classmethod: 作用是直接将自己的类对象,传给类方法. 一.classmethod 1)不用classmethod的时候 你的代码可能是这样写的, ...
- python中实现延时回调普通函数示例代码
python中实现延时回调普通函数示例代码 这篇文章主要给大家介绍了关于python中实现延时回调普通函数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的 ...
- Python中的startswith和endswith函数使用实例
Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...
- Python中的"缝合器"zip函数:将多个可迭代对象组合成一个迭代器
zip函数将参数中多个可迭代对象中相同序号的元素取出组合成一个元组作为输出列表的一个同样序号的元素,即输出列表的每个元素是一个元组,该元组的元素来源于参数中每个迭代对象的对应序号的元素. 具体可参考: ...
- python中dtype,type,astype的区别
python中dtype,type,astype的区别 type() dtype() astype() 函数名称 用法 type 返回参数的数据类型 dtype 返回数组中元素的数据类型 astype ...
随机推荐
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(14)-主框架搭建
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(14)-主框架搭建 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 (2 ...
- Redhat Linux下的python版本号升级
运行#Python与#python -V,看到版本是2.4.3,非常老了,并且之前写的都是跑在python3.X上面的,3.X和2.X有非常多不同, 有兴趣的朋友能够參考下这篇文章: http:// ...
- CSharp设计模式读书笔记(8):桥接模式(学习难度:★★★☆☆,使用频率:★★★☆☆)
桥接模式(Bridge Pattern): 将抽象部分与它的实现部分分离,使它们都可以独立地变化.它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模 ...
- HDU 3523 Image copy detection(KM最大匹配)
HDU 3523 Image copy detection 题目链接 题意:这题事实上题意读懂就简单了,说白了就是1-n放到1-n列,每列的值为每列上数字和该数字的差的绝对值,然后求总和最小 思路:就 ...
- 使用DPM(Deformable Part Model,voc-release3.1)算法INRIA通过训练你的身体检测模型数据集
我的环境 DPM源代码版本号:voc-release3.1 VOC开发包版本号:VOC2007_devkit_08-Jun Matlab版本号:MatlabR2012b c++编译器:VS2010 系 ...
- A generic error occurred in GDI+. 上传图片报错
代码就不说了,因为本地测试 ok, 服务端 就不行 ,服务器 环境 阿里云 win2008 r2 64 位 原因 是我没有这是 文件加权限 : 左边 的 少了 权限~ 代码 :含义是 网络图片 裁剪 ...
- Ubuntu更改hosts档
Ubuntu更改hosts档 打开hosts档 sudo gedit /etc/hosts 下载hosts,并全选复制 hosts 粘贴到hosts文件里.保存就可以 $(function () { ...
- 将已有的工程项目添加到Xcode到Git管理中
在Xcode中创建工程的时候,我们很容易的可以将新创建的工程添加到Git中,如图: 但是如果是本地已经有的工程,那该如何添加到Git中呢? 首先终端进入到该工程的目录. 然后: git init gi ...
- STL在迭代的过程,删除指定的元素
直接在Code.在 Picture #include <iostream> #include <list> using namespace std; // STL在迭代的过程中 ...
- IplImage 封装释放
IplImage是openCV库中非常重要的一个结构体,库中的图像都是保存为这个结构体后再进行操作的,详细结构例如以下: </pre><pre> typedef struct ...