python装饰器之staticmethod,classmethod,property 分类: python 2013-02-17 17:08 1970人阅读 评论(0) 收藏
2013-02-17 17:08
681 查看
文章出处:http://www.python123.com/thread-50-1-1.html。部分内容加以补充。
许久之前,一直不知道staticmethod,classmethod,property能做什么用处,不过说实话,这三个装饰器的用处也不是很大~
但是呢,偶尔还是会接触到相关的,所以还是弄清楚比较的好。
1. 先说说staticmethod吧
class Rabbit:
@staticmethod
def mystatic(wParam,lParam):
print 'this is a static method'
这里 mystatic 静态方法,和 c++里面的类静态方法基本一样。
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.Static methods in Python are similar to those
found in Java or C++.
===========================================
另外一种非静态方法:
class pin:
#@staticmethod
def pi(self):
return 'ok'
va=pin()
print va.pi() #如果在静态方法中执行该句,则需要把pi(self)中的self去掉
#print pin.pi() #非静态方法中,必须创建类的实例,通过实例调用方法。如果把此注释去掉,则会报错
===========================================
2.classmethod
请先看下例子输出:
class Rabbit(object):
def __init__(self,name):
self._name = name
@classmethod
def newClass(cls):
return 'abc',cls,Rabbit('')
=========================
或者:
r=Rabbit('am')
print r.newClass()
=========================
>>> Rabbit.newClass()
('abc', <class '__main__.Rabbit'>, <__main__.Rabbit object at 0x0259F130>)
仔细看下也就明白了,classmethod和普通的函数,就是把传入的self换成了cls对象,注意非任何实例。
3.property
细心的读者可能发现上面两个例子的类构造不一样,是的。porperty修饰器是针对新的对象方式的.
class Rabbit(object):
def __init__(self,name):
self._name = name
@classmethod
def newClass(cls):
return 'abc',cls,Rabbit('')
@property
def name(self):
return self._name
>>> ================================ RESTART ================================
>>>
>>> r = Rabbit('abc')
>>> r
<__main__.Rabbit object at 0x0258F130>
>>> r.name
'abc'
>>> r.name = '1'
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
r.name = '1'
AttributeError: can't set attribute
说白了,就是给函数设置只读属性。
这里定义的属性是一个只读属性,如果需要可写,则需要再定义一个setter:
class Rabbit(object):
def __init__(self,name):
self._name = name
@classmethod
def newClass(cls):
return 'abc',cls,Rabbit('')
@property
def name(self):
return self._name
@name.setter
def name(self,newname):
self._name= newname
另外说下:如果类定义的时候是 class Rabbit: ...这样的话,这个@property是米有只读限制的。建议大家都用新的类定义风格吧~
许久之前,一直不知道staticmethod,classmethod,property能做什么用处,不过说实话,这三个装饰器的用处也不是很大~
但是呢,偶尔还是会接触到相关的,所以还是弄清楚比较的好。
1. 先说说staticmethod吧
class Rabbit:
@staticmethod
def mystatic(wParam,lParam):
print 'this is a static method'
这里 mystatic 静态方法,和 c++里面的类静态方法基本一样。
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.Static methods in Python are similar to those
found in Java or C++.
===========================================
另外一种非静态方法:
class pin:
#@staticmethod
def pi(self):
return 'ok'
va=pin()
print va.pi() #如果在静态方法中执行该句,则需要把pi(self)中的self去掉
#print pin.pi() #非静态方法中,必须创建类的实例,通过实例调用方法。如果把此注释去掉,则会报错
===========================================
2.classmethod
请先看下例子输出:
class Rabbit(object):
def __init__(self,name):
self._name = name
@classmethod
def newClass(cls):
return 'abc',cls,Rabbit('')
=========================
或者:
r=Rabbit('am')
print r.newClass()
=========================
>>> Rabbit.newClass()
('abc', <class '__main__.Rabbit'>, <__main__.Rabbit object at 0x0259F130>)
仔细看下也就明白了,classmethod和普通的函数,就是把传入的self换成了cls对象,注意非任何实例。
3.property
细心的读者可能发现上面两个例子的类构造不一样,是的。porperty修饰器是针对新的对象方式的.
class Rabbit(object):
def __init__(self,name):
self._name = name
@classmethod
def newClass(cls):
return 'abc',cls,Rabbit('')
@property
def name(self):
return self._name
>>> ================================ RESTART ================================
>>>
>>> r = Rabbit('abc')
>>> r
<__main__.Rabbit object at 0x0258F130>
>>> r.name
'abc'
>>> r.name = '1'
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
r.name = '1'
AttributeError: can't set attribute
说白了,就是给函数设置只读属性。
这里定义的属性是一个只读属性,如果需要可写,则需要再定义一个setter:
class Rabbit(object):
def __init__(self,name):
self._name = name
@classmethod
def newClass(cls):
return 'abc',cls,Rabbit('')
@property
def name(self):
return self._name
@name.setter
def name(self,newname):
self._name= newname
另外说下:如果类定义的时候是 class Rabbit: ...这样的话,这个@property是米有只读限制的。建议大家都用新的类定义风格吧~
相关文章推荐
- Python装饰器学习(九步入门) 分类: python学习 2015-03-06 14:26 90人阅读 评论(0) 收藏
- tkinter显示当前时间 分类: python 2013-06-30 17:08 402人阅读 评论(0) 收藏
- python备份文件 分类: python 2013-03-15 12:00 365人阅读 评论(0) 收藏
- 优化程序结构 分类: divide into python python 小练习 2013-12-30 20:53 229人阅读 评论(0) 收藏
- file()创建文件方法 分类: python python基础学习 2012-12-24 18:17 239人阅读 评论(0) 收藏
- #小练习 使用HTMLParser获取data时注意事项 分类: python 小练习 HTMLParser 2013-11-08 21:05 335人阅读 评论(0) 收藏
- #小练习 解析HTML文件并使用字典保存链接 分类: HTMLParser python 小练习 2013-11-11 12:06 267人阅读 评论(0) 收藏
- Python生成8位随机字符串的一些方法 分类: python学习 2015-04-28 20:00 62人阅读 评论(0) 收藏
- 输出包含共同元素的变量值 分类: python 小练习 python基础学习 2014-03-11 13:55 188人阅读 评论(0) 收藏
- python 正则表达式操作指南 分类: 正则表达式 2013-05-28 16:18 384人阅读 评论(0) 收藏
- python 调用shell命令三种方法 分类: python基础学习 python Module 2014-01-13 14:13 239人阅读 评论(0) 收藏
- 命令行执行python模块时提示ImportError: No module named xxx 分类: python学习 2014-07-24 11:48 868人阅读 评论(0) 收藏
- Ubuntu 12.04 安装mysql及mysql-python 分类: python Module ubuntu database 2014-01-23 10:07 615人阅读 评论(0) 收藏
- Python修炼--常用字符串、数学函数 分类: python基础学习 2013-06-16 13:23 205人阅读 评论(0) 收藏
- 使用set()求出列表交集 分类: python基础学习 2013-06-16 17:00 241人阅读 评论(0) 收藏
- 最大公约数 分类: python 小练习 2013-12-02 11:00 222人阅读 评论(0) 收藏
- python中计时工具timeit模块的基本用法 分类: python python基础学习 2013-08-08 10:05 2072人阅读 评论(0) 收藏
- 结尾非零数的奇偶性 分类: python 小练习 2013-12-02 18:04 371人阅读 评论(0) 收藏
- 去除字符串重复数据 分类: python基础学习 2013-08-08 17:43 171人阅读 评论(0) 收藏
- 使用python下载文件 分类: python python下载 2013-06-22 16:58 277人阅读 评论(0) 收藏