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

通过代码学习python之@property,@staticmethod,@classmethod的更多相关文章

  1. python之内置装饰器(property/staticmethod/classmethod)

    python内置了property.staticmethod.classmethod三个装饰器,有时候我们也会用到,这里简单说明下 1.property 作用:顾名思义把函数装饰成属性 一般我们调用类 ...

  2. 《Python》 property、classmethod、staticmethod、isinstance、issubclass

    一.property property是一个装饰器函数 装饰器函数的使用方法:在函数.方法.类的上面一行直接@装饰器的名字 装饰器的分类: 1.装饰函数 2.装饰方法:property 3.装饰类 i ...

  3. Python中@property和@classmethod和@staticmethod

    前戏 首先,先要弄清楚一个类里面的,各个组成部分都应该怎么称呼. - 注:可能叫法会不太一样. 关于@property 顾名思义:它的意思为‘属性’. 作用: 1:使用它你将会把类方法,变为类属性.并 ...

  4. python 类中staticmethod,classmethod,普通方法

    1.staticmethod:静态方法和全局函数类似,但是通过类和对象调用. 2.classmethod:类方法和类相关的方法,第一个参数是class对象(不是实例对象).在python中class也 ...

  5. Python中的 @staticmethod@classmethod方法

    python类中有三种方法,常见的是实例方法,另外两种是staticmethod装饰的静态方法,和classmethod装饰的类方法. 1.对比 流畅的python里,用一个例子进行了对比: (1)两 ...

  6. 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别

    [面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...

  7. python基础知识讲解——@classmethod和@staticmethod的作用

    python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...

  8. python 学习笔记之@property

    今天学习python类看到 @property 的用法觉得很新奇,就自己尝试了很久,刚开始不明白,后来终于明白了点 其实总结一句话就是, @property 把类中的方法调用方式改变成当成属性属性调用 ...

  9. Python staticmethod classmethod 普通方法 类变量 实例变量 cls self 概念与区别

    类变量 1.需要在一个类的各个对象间交互,即需要一个数据对象为整个类而非某个对象服务. 2.同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见. 3.有独立的存储区,属于整个类.   ...

随机推荐

  1. C头文件中尖括号与双引号的区别及编译搜索顺序

    这两天被问到一个很有意思的问题:C头文件中尖括号与双引号有什么区别,以前只大约知道 <> 常用在系统库文件,"" 常用在自定义的借口文件中,那具体在gcc编译搜索过程中 ...

  2. c语言—栈区,堆区,全局区,文字常量区,程序代码区 详解

    转:http://www.cnblogs.com/xiaowenhui/p/4669684.html 一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分1.栈区(sta ...

  3. 题解报告:poj 1426 Find The Multiple(bfs、dfs)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  4. scala学习笔记1: scala method

    刚接触scala,做练习的时候碰到一个问题,顺便mark一下. 先看下面一段代码: def sum(args:Int*) = { var result = 0 for (arg <- args) ...

  5. Python的I/O操作

    1.读取键盘输入 msg = raw_input("Please enter :") print "you input ",msg #可接受Python表达式作 ...

  6. LN : JSON (利用C++实现JSON)

    Appreciation to our TA, 王毅峰, who designed this task. 问题描述 JSON, JavaScript Object Notation,is an fle ...

  7. mvc使用linq to sql进行sum统计遇到查询为null的问题

    mvc linq to sql,linq to entity,sum,null 昨天写了段sum的统计语句, decimal sums sums = ( from fac in db.Apply wh ...

  8. 安装mask-rcnn问题汇总

    1. I download file from https://github.com/waleedka/coco. Then I placed the file in Mask_RCNN-master ...

  9. (转)版本管理工具介绍——SVN篇(一)

    http://blog.csdn.net/yerenyuan_pku/article/details/72620101 SVN是何物 SVN是Subversion的简称,是一款集中式的开源版本控制系统 ...

  10. dutacm.club_1089_A Water Problem_(dp)

    题意:要获得刚好后n个'k'的字符串,有两种操作,1.花费x秒,增加或删除1个'k'; 2.花费y秒,使个数翻倍.问最少需要多少时间获得这个字符串. 思路:i为偶数个'k',dp[i]=min(dp[ ...