python中staticmethod classmethod及普通函数的区别

简介: http://genggeng.iteye.com/blog/1290458 staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象(python里光说对象总是容易产生混淆, 因为什么都是对象,包括类,而实际上类实例对象才是对应静态语言中所谓对象的东西)来调用而已, 不会隐式地传入任何参数。
+关注继续查看
http://genggeng.iteye.com/blog/1290458

staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象(python里光说对象总是容易产生混淆, 因为什么都是对象,包括类,而实际上类实例对象才是对应静态语言中所谓对象的东西)来调用而已, 不会隐式地传入任何参数。这个和静态语言中的静态方法比较像。


classmethod 是和一个class相关的方法,可以通过类或类实例调用,并将该class对象(不是class的实例对象)隐式地 当作第一个参数传入。就这种方法可能会比较奇怪一点,不过只要你搞清楚了python里class也是个真实地 存在于内存中的对象,而不是静态语言中只存在于编译期间的类型。


正常的方法就是和一个类的实例对象相关的方法,通过类实例对象进行调用,并将该实例对象隐式地作为第一 个参数传入,这个也和其它语言比较像。


可如下示例:


Python代码  收藏代码
  1. #!/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:


Shell代码  收藏代码
  1. 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  
目录
相关文章
|
2天前
|
Python
在Python云函数中使用Flask时
在Python云函数中使用Flask时
15 6
|
4天前
|
Python
关于Python的Numpy库reshape()函数的用法
1.介绍 更改数组的形状,不改变原数组 2.语法 a = np.reshape(mat, newshape, order = ‘C’) a : newshape形状的新数组 mat : 原数组
13 0
|
5天前
|
搜索推荐 算法 Python
如何实现归并排序算法? 要求:编写一个Python函数,输入一个无序列表,返回排序后的列表。
如何实现归并排序算法? 要求:编写一个Python函数,输入一个无序列表,返回排序后的列表。
|
5天前
|
算法 索引 Python
如何实现二分查找算法? 要求:编写一个Python函数,输入一个有序列表和一个目标值,返回目标值在列表中的索引。如果目标值不在列表中,返回-1。
如何实现二分查找算法? 要求:编写一个Python函数,输入一个有序列表和一个目标值,返回目标值在列表中的索引。如果目标值不在列表中,返回-1。
|
5天前
|
Python
如何判断一个数是质数? 要求:编写一个Python函数,输入一个整数,输出该整数是否为质数。质数是指大于1的自然数中,除了1和它本身以外不再有其他因数的数。
如何判断一个数是质数? 要求:编写一个Python函数,输入一个整数,输出该整数是否为质数。质数是指大于1的自然数中,除了1和它本身以外不再有其他因数的数。
|
8月前
|
Python
Python @classmethod和@staticmethod装饰器使用介绍
Python @classmethod和@staticmethod装饰器使用介绍
49 0
|
Python
python小知识-classmethod类方法
类方法:@classmethod装饰的方法,参数有一个隐含参数cls,表示类本身,类可以直接调用
182 0
|
Java Python
python之 @staticmethod 和 @classmethod
python之 @staticmethod 和 @classmethod
93 0
python之 @staticmethod 和 @classmethod
|
C# 开发者 Python
【开发者笔记】python中的类方法(@classmethod)和静态方法(@staticmethod)
在java、c#等高级语言中我们用static来定义静态方法和静态变量,那么在python中如何定义静态方法和静态变量呢。 python提供了@classmethod和@staticmethod来定义静态方法,刚接触的时候不太明白,Stack Overflow提供了一个比较方便理解的解释,Stack Overflow回答。
1621 0
|
Web App开发 测试技术 Python
Selenium2+python自动化55-unittest之装饰器(@classmethod)
前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间。 于是就想是不是可以只打开一次浏览器,执行完用例再关闭呢?这就需要用到装饰器(@classmethod)来解决了。
1426 0
相关产品
云迁移中心
推荐文章
更多