通过代码学习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._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的更多相关文章
- python之内置装饰器(property/staticmethod/classmethod)
python内置了property.staticmethod.classmethod三个装饰器,有时候我们也会用到,这里简单说明下 1.property 作用:顾名思义把函数装饰成属性 一般我们调用类 ...
- 《Python》 property、classmethod、staticmethod、isinstance、issubclass
一.property property是一个装饰器函数 装饰器函数的使用方法:在函数.方法.类的上面一行直接@装饰器的名字 装饰器的分类: 1.装饰函数 2.装饰方法:property 3.装饰类 i ...
- Python中@property和@classmethod和@staticmethod
前戏 首先,先要弄清楚一个类里面的,各个组成部分都应该怎么称呼. - 注:可能叫法会不太一样. 关于@property 顾名思义:它的意思为‘属性’. 作用: 1:使用它你将会把类方法,变为类属性.并 ...
- python 类中staticmethod,classmethod,普通方法
1.staticmethod:静态方法和全局函数类似,但是通过类和对象调用. 2.classmethod:类方法和类相关的方法,第一个参数是class对象(不是实例对象).在python中class也 ...
- Python中的 @staticmethod@classmethod方法
python类中有三种方法,常见的是实例方法,另外两种是staticmethod装饰的静态方法,和classmethod装饰的类方法. 1.对比 流畅的python里,用一个例子进行了对比: (1)两 ...
- 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别
[面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...
- python基础知识讲解——@classmethod和@staticmethod的作用
python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...
- python 学习笔记之@property
今天学习python类看到 @property 的用法觉得很新奇,就自己尝试了很久,刚开始不明白,后来终于明白了点 其实总结一句话就是, @property 把类中的方法调用方式改变成当成属性属性调用 ...
- Python staticmethod classmethod 普通方法 类变量 实例变量 cls self 概念与区别
类变量 1.需要在一个类的各个对象间交互,即需要一个数据对象为整个类而非某个对象服务. 2.同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见. 3.有独立的存储区,属于整个类. ...
随机推荐
- knockout 和mvc4结合使用
Knockout (或者Knockout.js ,KnockoutJS)是一个开源的JavaScript库,网址为www.knockoutjs.com.Knockout语法简洁.可读性好,能轻松实现与 ...
- 全面学习ORACLE Scheduler特性(3)使用Programs
二.使用Programs 在论坛中偶尔见过有人讨论如何在ORACLE中执行操作系统命令,或是ORACLE数据库外的应用.应该说在9i及之前的版本中,虽然说并非完全无法实现(其实还是有多种方式能够变相实 ...
- 几个不同的tab切换示例
上一篇<论tab切换的几种实现方法>中讲了tab切换的4种不同实现原理,那么,现在到理论联系实际的时候了,下面就写几个实例. 一.仿”中国人民大学“官网的tab切换,背景是图片,效果图如下 ...
- IKanalyzer、ansj_seg、jcseg三种中文分词器的实战较量
转自:http://lies-joker.iteye.com/blog/2173086 选手:IKanalyzer.ansj_seg.jcseg 硬件:i5-3470 3.2GHz 8GB win7 ...
- UNICODE本地编译freescale的i.MX6Q的android4.2.2&android4.4.2 && 全志a80的步骤x1
20151031本地编译freescale的i.MX6Q的android4.2.2&android4.4.2 && 全志a80的步骤x1 2015/10/31 15:07 开始 ...
- Android中出现Error:In (declare-styleable) FontFamilyFont, unable to find attribute android:font
Android中出现Error:In (declare-styleable) FontFamilyFont, unable to find attribute android:font 解决办法,今天 ...
- Fiddler——抓包工具的使用
fiddler安装 pc端安装fiddler,自行从百度下载即可 Fiddler是强大且好用的Web调试工具之一,它能记录客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输 ...
- 【译】x86程序员手册27-7.6任务链
7.6 Task Linking 任务链 The back-link field of the TSS and the NT (nested task) bit of the flag word to ...
- 如何利用CSS中的ime-mode用来控制页面上文本框中的全角/半角输入
css 之 ime-mode语法:ime-mode : auto | active | inactive | disabled取值:auto : 默认值.不影响ime的状态.与不指定 ime-mode ...
- Codeforces_776_C_(思维)(前缀和)
C. Molly's Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes input stand ...