the5fire的技术博客

关注Python、Django、Vim、Linux、Web开发、团队管理和互联网--Life is short, we need Python.


python之@property,@staticmethod,@classmethod

作者:the5fire | 标签:     | 发布:2012-07-16 10:42 p.m. | 阅读量: 9346, 9186

通过下面这个代码大概就可以了解到这三个装饰器的作用

#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'
- from the5fire.com
----EOF-----

微信公众号:Python程序员杂谈



其他分类: