staticmethod classmethod修饰符
一、staticmethod(function)
Return a static method for function.
A static method does not receive an implicit first argument. To declare a static method, use this idiom:
class Sing():
@staticmethod
def sayHello():
print "hello world" def sayNo():
print "no" if __name__ == '__main__':
Sing.sayHello()
Sing.sayNo()
Sing.sayNo报错如下:
TypeError: unbound method sayNo() must be called with Sing instance as first argument (got nothing instead)
sayNo()方法非静态方法,需要先将对象Sing实例化才能调用该方法
而sayHello()方法不报错,因为staticmethod修饰
下面方法才是对的:
class Sing():
@staticmethod
def sayHello():
print "hello world" def sayNo(self):
print "no" if __name__ == '__main__':
Sing.sayHello()
sing = Sing()
sing.sayNo()
二、classmethod(function)
Return a class method for function.
A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:
class Sing():
@classmethod
def sayHello(self):
print "hello world" def sayNo(self):
print "no" if __name__ == '__main__':
Sing.sayHello()
sing = Sing()
sing.sayNo()
注意:staticmethod修饰的方法不需要接self参数,而classmethod修饰的方法则需要接self参数;
三、@property修饰符(新式类特有,新式类即接object定义的类)
修饰为类的属性,如下为不加@property修饰符的类的定义:
class C(object):
def __init__(self):
self._x = None def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")
通过@property修饰符修饰的类的定义:(从Python2.6开始引入setter,deleter,getter属性)
class C(object):
def __init__(self): self._x = None @property
def x(self):
"""I'm the 'x' property."""
return self._x @x.setter
def x(self, value):
self._x = value @x.deleter
def x(self):
del self._x
staticmethod classmethod修饰符的更多相关文章
- Python classmethod 修饰符
描述 classmethod修饰符对应的函数不需要实例化,不需要self参数,但第一个参数需要是表示自身类的cls参数,可以调用类的属性,类的方法,实例化对象等. 语法 classmethod语法: ...
- @classmethod和@staticmethod修饰符
@classmethod和@staticmethod 一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直 ...
- python_way,day8 面向对象【多态、成员--字段 方法 属性、成员修饰符、特殊成员、异常处理、设计模式之单例模式、模块:isinstance、issubclass】
python_way day8 一.面向对象三大特性: 多态 二.面向对象中的成员 字段.方法属性 三.成员修饰符 四.特殊成员 __init__.__doc__.__call__.__setitem ...
- Python_day8_面向对象(多态、成员修饰符、类中特殊方法、对象边缘知识)、异常处理之篇
一.面向对象之多态 1.多态:简而言子就是多种形态或多种类型 python中不支持多态也用不到多态,多态的概念是应用与java/C#中指定传参的数据类型, java多态传参:必须是传参数的数据类型或传 ...
- python中的 @ 修饰符
今天学习廖老师的python教程,碰到了修饰符'@',不太了解,查看了下官方文档. 简单的整理下: @dec2 @dec1 def func(arg1, arg2, ...): pass 等价于 de ...
- python staticmethod classmethod
http://www.cnblogs.com/chenzehe/archive/2010/09/01/1814639.html classmethod:类方法staticmethod:静态方法 在py ...
- 文成小盆友python-num8 面向对象中的成员,成员修饰符,特殊成员,异常处理,设计模式之单例模式
本节主要内容: 1.面向对象中的成员 2.成员修饰符 3.特殊成员 4.异常处理 5.设计模式之单例模式 一.面向对象中的成员(类的成员) 类的成员总共可以分为3大类,每类中有不同的分支. 1.总述, ...
- python学习day20 面向对象(二)类成员&成员修饰符
1.成员 类成员 类变量 绑定方法 类方法 静态方法 属性 实例成员(对象) 实例变量 1.1实例变量 类实例化后的对象内部的变量 1.2类变量 类中的变量,写在类的下一级和方法同一级. 访问方法: ...
- Python 函数修饰符(装饰器)的使用
Python 函数修饰符(装饰器)的使用 1. 修饰符的来源修饰符是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等. 修饰符是解决这类问题的绝佳设计, ...
随机推荐
- CentOS7.x 通过mail命令发,使用465端口(smtps协议)发送邮件
#创建证书mkdir -p /root/.certs/echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CE ...
- ElasticSearch 数据类型
1.范围数据类型 支持以下范围类型: integer_range : 一系列带符号的32位整数,最小值为,最大值为 float_range:一系列单精度32位IEEE 754浮点值. long_ran ...
- 【OpenStack项目管理-CPU/内存/存储/网络 配额管理】
参考资料: OpenStack如何管理项目和用户:http://os.51cto.com/art/201312/422010.htm Nova.Cinder.Neutron资源配额设置:http:// ...
- There is insufficient memory for the Java Runtime Environment to continue问题解决
在linux系统下长时间进行性能測试,连续几次发生server假死无法连接上的情况,无奈仅仅能重新启动server.在測试路径下发现hs_err_pid17285.log文件,打开文件查看其主要内容例 ...
- Unity导出webPlayer并且部署到IIS
http://blog.csdn.net/zooen2011/article/details/12884811 做好的Unity3D项目工程导出webPlayer类型,本地可以直接打开导出的html文 ...
- 使用OpenSSL生成CSR文件,并申请全球通用SSL证书
http://www.openssl.org 上只有OpenSSL的原代码下载,为了方便Windows用户使用OpenSSL,我们特地为您准备了OpenSSL 0.9.8.a for win32的可执 ...
- iOS开发-XCode常用快捷键整理
前言:如果我们能够掌握并巧妙地使用快捷键,可以大大加快我们的工作效率,这个对经常使用快捷键的人们来说,应该很容易理解.因此我们需要做的是,针对于自己经常使用的快捷键去进行记忆.我不会推荐你们去把所有的 ...
- lua类库 middleclass学习笔记
middleclass使在lua中面象对象变的简单 抄了一遍他的示例代码运行着试了试,基本懂了 local class = require 'middleclass' --类的继承 Person = ...
- Lomboz插件的介绍 下载 安装 问题
http://www.blogjava.net/javaandcc/articles/251334.html Lomboz是Eclipse的一个主要的开源插件(open-source plug-in) ...
- struts2学习笔记(一)
1.搭建第一个struts2 app. web.xml <?xml version="1.0" encoding="UTF-8"?> <we ...