通过代码学习python之@property,@staticmethod,@classmethod

简介: URL: https://www.the5fire.com/python-property-staticmethod-classmethod.html #coding=utf-8 class MyClass(object): def __init__(self): print 'init' self.
+关注继续查看

URL:

https://www.the5fire.com/python-property-staticmethod-classmethod.html

#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__':
    print 'hahahah'
    MyClass.static_method()
    MyClass.class_method()
    mc = MyClass()
    mc.test()
    print mc.name
    # mc.name = 'huyang'
    # print mc.name

目录
相关文章
|
3天前
|
Python
【Python学习】—面向对象(九)
【Python学习】—面向对象(九)
|
3天前
|
JSON 数据可视化 数据挖掘
【Python学习】—Python数据可视化(八)
【Python学习】—Python数据可视化(八)
|
3天前
|
Python
【Python学习】—Python基础语法(七)
【Python学习】—Python基础语法(七)
|
3天前
|
存储 索引 Python
【Python学习】—Python基础语法(六)
【Python学习】—Python基础语法(六)
|
3天前
|
Python
【Python学习】—Python基础语法(五)
【Python学习】—Python基础语法(五)
|
3天前
|
Python
【Python学习】—Python基础语法(四)
【Python学习】—Python基础语法(四)
【Python学习】—Python基础语法(四)
|
3天前
|
Python
【Python学习】—Python基础语法(三)
【Python学习】—Python基础语法(三)
|
3天前
|
小程序 Python
【Python学习】—Python基础语法(二)
【Python学习】—Python基础语法(二)
|
3天前
|
Python
【Python学习】—认识Python与环境搭建(一)
【Python学习】—认识Python与环境搭建(一)
|
5天前
|
机器学习/深度学习 数据挖掘 程序员
Python学习难度的具体标准是什么?
Python学习难度的具体标准是什么?
相关产品
云迁移中心
推荐文章
更多