`

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

 
阅读更多

 

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

 

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

 

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

 

可如下示例:

#!/usr/bin/python
#coding:utf-8

#author:	gavingeng
#date:		2011-12-03 10:50:01 

class Person:

	def __init__(self):
		print "init"

	@staticmethod
	def sayHello(hello):
		if not hello:
			hello='hello'
		print "i will sya %s" %hello


	@classmethod
	def introduce(clazz,hello):
		clazz.sayHello(hello)
		print "from introduce method"

	def hello(self,hello):
		self.sayHello(hello)
		print "from hello method"		


def main():
	Person.sayHello("haha")
	Person.introduce("hello world!")
	#Person.hello("self.hello")	#TypeError: unbound method hello() must be called with Person instance as first argument (got str instance instead)
	
	print "*" * 20
	p = Person()
	p.sayHello("haha")
	p.introduce("hello world!")
	p.hello("self.hello")

if __name__=='__main__':
	main()

 

output:

i will sya haha
i will sya hello world!
from introduce method
********************
init
i will sya haha
i will sya hello world!
from introduce method
i will sya self.hello
from hello method
 
分享到:
评论
1 楼 prayjourney 2016-05-21  
不错   

相关推荐

    Python中staticmethod和classmethod的作用与区别

    今天小编就为大家分享一篇关于Python中staticmethod和classmethod的作用与区别,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    基于python中staticmethod和classmethod的区别(详解)

    下面小编就为大家带来一篇基于python中staticmethod和classmethod的区别(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Python @函数装饰器及@staticmethod,@classmethod.docx

    使用 @ 符号引用已有的函数(比如 @staticmethod、@classmethod)后,可用于修饰其他函数,装饰被修饰的函数。那么我们是否可以开发自定义的函数装饰器呢? 答案是肯定的。当程序使用“@函数”(比如函数 A)...

    python的staticmethod与classmethod实现实例代码

    主要介绍了python的staticmethod与classmethod实现实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下

    Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析

    主要介绍了Python 类方法和实例方法(@classmethod),静态方法(@staticmethod),结合实例形式分析了Python 类方法和实例方法及静态方法相关原理、用法及相关操作注意事项,需要的朋友可以参考下

    超全面python面试题

    3 @staticmethod和@classmethod 4 类变量和实例变量 5 Python自省 6 字典推导式 7 Python中单下划线和双下划线 8 字符串格式化:\x和.format 9 迭代器和生成器 10 *args and **kwargs 11 面向切面编程AOP和装饰器 12 ...

    Python 静态方法和类方法实例分析

    主要介绍了Python 静态方法和类方法,结合实例形式分析了Python类、实例、静态方法等相关概念、原理与使用技巧,需要的朋友可以参考下

    Python面试内容很多,干货满满

    3 @staticmethod和@classmethod 。 4 类变量和实例变量 o 5 Python自省 。6字典推导式 。 7 Python中单下划线和双下划线 。8 字符串格式化:\x和.format 。 9 选代器和生成器 10*args and **kwargs 。 11 面向切面...

    深入浅析python 中的self和cls的区别

    python 中的self和cls 一句话描述:self是类(Class)实例化对象,cls就是类(或子类)本身,取决于调用的是那个类。 @staticmethod 属于静态方法装饰器,@classmethod属于类方法装饰器。我们需要从声明和使用两个...

    python练手经典100例-推荐几个适合新手练手的Python项目《python入门例子》.pdf

    python练⼿经典100例-推荐⼏个适合新⼿练⼿的Python项⽬ 《python⼊门例⼦》 python 为什么实例对象可以调⽤类⽅法? 实例是什么例是类定义的实。那么,类中所定义的属⽅只要没有被屏蔽,在它的实体中就同样是可访问...

    Python基于staticmethod装饰器标示静态方法

    staticmethod(function) Return a static method for function. A static method does not receive an implicit first argument. The @staticmethod form is a function decorator – see the description of ...

    Python3.6.x中内置函数总结及讲解

    Python3.6.x中内置函数总结 # -*- coding:utf-8 -*- """ abs() dict() help() min() setattr() all() dir() hex() next() slice() any() divmod() id() object() sorted() ascii() enumerate() input() oct() ...

    python装饰器decorator介绍

    python中的decorator通常为输入一个函数,经过装饰后返回另一个函数。 比较常用的功能一般使用decorator来实现,例如python自带的staticmethod和classmethod。 装饰器有两种形式: 复制代码 代码如下: @A def foo()...

    python实现静态方法工具类

    在这个文件中,可以像 Java 中的静态方法一样,定义函数或类方法,只需在函数名或方法名前加上 @staticmethod 或者不加,即可定义成静态方法,无需实例化相关对象即可直接调用。 在 Python 中,可以使用模块(Module...

    1 python的三种类方法

    Python对象里有三类方法: 1 类方法 @classmethod :基于类的一些属性...2 静态方法 @staticmethod: 本质上和普通函数没什么不同,归集到一个类下面,相当于整理 3 实例方法 :需要结合对象的实例使用,第一个参数是cls

    详解Python中的静态方法与类成员方法

    对Python中的类,类之间的组织关系,整个项目中类之间如何耦合还缺乏认识。打算读一读别人写的Python代码来学习一下Python在工程中的应用,提升自己的技术水平。选取的Python代码是Python爬虫代码,github地址。这个...

    Python探索之静态方法和类方法的区别详解

    python staticmethod and classmethod Though classmethod and staticmethod are quite similar, there's a slight difference in usage for both entities: classmethod must have a reference to a class object ...

    Python核心编程第二版

     4.6.5 Python类型操作符和内建函数总结   4.7 类型工厂函数   4.8 标准类型的分类   4.8.1 存储模型   4.8.2 更新模型   4.8.3 访问模型   4.9 不支持的类型   4.10 练习   第5章 数字 ...

    Python3 菜鸟查询手册

    01.01 2.x与3.x版本区别.png 02 基础语法.png 02.01 命令行参数.png 03 基本数据类型.png 03.01 数据类型转换 int() 函数.png 03.02 数据类型转换 float() 函数.png 03.03 数据类型转换 complex() 函数.png ...

    Python核心编程第二版(ok)

     4.6.5 Python类型操作符和内建函数总结   4.7 类型工厂函数   4.8 标准类型的分类   4.8.1 存储模型   4.8.2 更新模型   4.8.3 访问模型   4.9 不支持的类型   4.10 练习   第5章 数字 ...

Global site tag (gtag.js) - Google Analytics