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  
目录
相关文章
|
9天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
13 1
|
1天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
30 1
|
2天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
|
3天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
25 0
|
3天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
5天前
|
Python
python学习10-函数
python学习10-函数
|
5天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
8天前
|
测试技术 开发者 Python
Python中的装饰器:优雅而强大的函数修饰工具
在Python编程中,装饰器是一种强大的工具,用于修改函数或方法的行为。本文将深入探讨Python中装饰器的概念、用法和实际应用,以及如何利用装饰器实现代码的优雅和高效。
|
12天前
|
Python
Python函数学习应用案例详解
【4月更文挑战第7天】学习Python函数的应用,包括计算两数之和、判断偶数、计算阶乘、生成斐波那契数列及反转字符串。示例代码展示了函数接收参数和返回结果的功能,如`add(a, b)`求和,`is_even(num)`判断偶数,`factorial(n)`计算阶乘,`fibonacci(n)`生成斐波那契数,以及`reverse_string(s)`反转字符串。
13 1
|
13天前
|
Python
python基础篇:什么是函数?函数有什么用?
python基础篇:什么是函数?函数有什么用?
25 3