通过代码学习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.有独立的存储区,属于整个类. ...
随机推荐
- 题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 377 Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 10步成为专业iOS开发者——新手向,从零起步
想在移动开发大餐中分一杯羹,体验最火最炫的技术?小伙子,很有眼光嘛 毫无疑问,移动开发在目前和未来几年内都会盛极一时.无数开发职位虚位以待.各大公司都在寻找各种层次的程序员——新手级.入门级.中级.老 ...
- PHP语言开发Paypal支付demo的具体实现
如果我们的应用是面向国际的.那么支付的时候通常会考虑使用paypal.以下为个人写的一个paypal支付示例,已亲测可行.paypal有个很不错的地方就是为开发者提供了sandbox(沙箱)测试功能. ...
- CF816B Karen and Coffee
思路: 有点类似于区间修改点查询的树状数组. 实现: #include <iostream> #include <cstdio> using namespace std; ; ...
- 完成fcc作业2时思路
1.设置导航链接按钮栏时,不能用文档流,要用position:fixed;固定在窗口上方, 其他普通流盒子按上下顺序就用position:relative:后面发现导航栏被普通流盒子挡在了下面,就设置 ...
- intellij idea console 乱码
修改文件 位置:{用户目录}\{iedea对应版本}\{idea or idea64}.vmoptions 比如我要修改我的配置文件 C:\Users\kkblf\.IntelliJIdea2017. ...
- C++为什么抓不到除0错“异常”?
http://blog.csdn.net/nanyu/article/details/6475555 有人问这个问题: try { std::cout << 10/0 << s ...
- 【前端路由】Vue-router 中hash模式和history模式的区别
咱们今天说说VUE路由的hash模式与history模式的区别,这个也是面试常问的问题,不要小看这道题其实问到这里的时候那个面试官应该是个大牛,开发经验丰富,这个题其实就是考验你的开发经验是否属实. ...
- CAD得到所有实体1
主要用到函数说明: IMxDrawSelectionSet::AllSelect 得到当前空间的所有实体.详细说明如下: 参数 说明 [in,defaultvalue(NULL)] IMxDrawRe ...