前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python之@property,@staticmethod,@classmethod

python之@property,@staticmethod,@classmethod

作者头像
the5fire
发布2019-02-28 16:14:40
5090
发布2019-02-28 16:14:40
举报

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

#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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-07-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档