(python 1)python中staticmethod函数、内建函数setattr
来源:互联网 发布:unity3d asset store 编辑:程序博客网 时间:2024/06/15 16:03
python中staticmethod classmethod、普通函数的区别
staticmethod基本上和一个全局函数差不多,只不过可以通过类或类的实例对象(python里光说对象总是容易产生混淆,因为什么都是对象,包括类,而实际上类实例对象才是对应静态语言中所谓对象的东西)来调用而已,不会隐式地传入任何参数。这个和静态语言中的静态方法比较像。
classmethod是和一个class相关的方法,可以通过类或类实例调用,并将该class对象(不是class的实例对象)隐式地当作第一个参数传入。就这种方法可能会比较奇怪一点,不过只要你搞清楚了python里class也是个真实地存在于内存中的对象,而不是静态语言中只存在于编译期间的类型。
正常的方法就是和一个类的实例对象相关的方法,通过类实例对象进行调用,并将该实例对象隐式地作为第一个参数传入,这个也和其它语言比较像。
#!/usr/bin/python#coding:utf-8#author: gavingeng#date: 2016-06-16 10:50:01 class Person: def __init__(self): print ("init") @staticmethod def sayHello(hello): if not hello: hello='hello' print ("i will sya {0}".format(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") #通过类调用staticmethod方法 Person.introduce("hello world!") #通过类调用@classmethod方法 #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()运行结果:
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
[Finished in 0.1s]
内建函数setattr的使用
http://www.cnblogs.com/zhangjing0502/archive/2012/05/16/2503702.html
setattr(
object, name, value)
This is the counterpart of getattr(). The arguments
are an object, a string and an arbitrary value. The string may name an existing
attribute or a new attribute. The function assigns the value to the attribute,
provided the object allows it. For example, setattr(x,
'foobar', 123) is equivalent to
x.foobar = 123.
这是相对应的getattr()。参数是一个对象,一个字符串和一个任意值。字符串可能会列出一个现有的属性或一个新的属性。这个函数将值赋给属性的。该对象允许它提供。例如,setattr(x,“foobar”,123)相当于x.foobar = 123。
- (python 1)python中staticmethod函数、内建函数setattr
- python: hasattr()、setattr()、getattr()、delattr() 内建函数
- python setattr 函数
- python 面向对象中一些常用内建函数【getattr(), setattr(). hasattr(). delattr()...】
- Python中hasattr() getattr() setattr() 函数使用方法详解
- PYTHON getattr setattr hasattr delattr内置函数
- python hasattr() getattr() setattr()函数的使用
- Python的hasattr() getattr() setattr() 函数使用方法
- python中staticmethod classmethod及普通函数的区别
- python中staticmethod classmethod及普通函数的区别
- python中staticmethod classmethod及普通函数的区别
- Python标准库:内置函数staticmethod(function)
- Python内置函数的staticmethod()与classmethod(),@staticmethod,@classmethod
- python起步 内建函数(1)
- Python 内建函数
- Python内建函数
- Python内建函数
- Python 内建函数
- 算法导论活动选择问题
- SAP WebService基础知识
- Linux系统常用命令
- 浅谈 JS 创建对象的 8 种模式
- Jetson TK1 @ubuntu14.04 opencv 双目相机
- (python 1)python中staticmethod函数、内建函数setattr
- Retrolambda 混淆配置
- 在div宽高不确定的情况下,使其居中[轮播图点击按钮常用]——前端工程师和派孔明
- 四大组件之ContentProvider(一)-使用系统提供的ContentProvider
- 美欧科学家清晰地探测到引力波
- Django快速搭建Blog
- 前端发展薪资等杂七杂八
- UIWebView保存图片
- <Android>自定义Log开关