the5fire的技术博客

关注python、vim、linux、web开发和互联网--life is short, we need python.


python之@property,@staticmethod,@classmethod

作者:the5fire | 标签:     | 发布:2012-07-16 10:42 p.m. | 阅读量: 8055, 8000
通过下面这个代码大概就可以了解到这三个装饰器的作用

#coding=utf-8

class MyClass(object):
def __init__(self):
print 'init'
self._name = 'the5fire'

@staticmethod
def static_method():
print 'This is a static method!'

@classmethod
def class_method(cls):
print 'This is a class method',cls
print 'visit the property of the class:',cls.name
print 'visit the static method of the class:',cls.static_method()
instance = cls()
print 'visit the normal method of the class:',instance.test()


def test(self):
print 'call test'

@property
def name(self):
return self._name

if __name__ == '__main__':
MyClass.static_method()
MyClass.class_method()
mc = MyClass()
print mc.name
mc.name = 'huyang'

----EOF-----

扫码关注,或者搜索微信公众号:码农悟凡


其他分类: