python -- @classmethod @staticmethod区别和使用
python中的定义:
- class MyClass:
- ...
- @classmethod # classmethod的修饰符
- def class_method(cls, arg1, arg2, ...):
- ...
- @staticmethod # staticmethod的修饰符
- def static_method(arg1, arg2, ...):
- ...
@classmethod : 类方法
@staticmethod : 静态方法
类方法和静态方法的调用一样,都是通过类就可以直接调用。
区别:类方法,需要传入该类,定义类方法的时候要传一个默认的参数cls。静态方法则不用。
示例:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#!/usr/bin/env python# -*- coding: utf-8 -*-# blog.ithomer.netclass Test(object): x = 11 def __init__(self, _x): self._x = _x print("Test.__init__") @classmethod def class_method(cls): print("class_method") @staticmethod def static_method(): print("static_method") @classmethod def getPt(cls): cls.class_method() cls.static_method() if "__main__" == __name__: Test.class_method() # class_method Test.static_method() # static_method Test.getPt() # class_method static_method t = Test(22) # Test.__init__ t.class_method() # class_method t.static_method() # static_method print Test.x # 11# print Test._x print t.x # 11 print t._x # 22 # t.getPr() # 'Test' object has no attribute 'getPr' |
运行结果:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class_methodstatic_methodclass_methodstatic_methodTest.__init__class_methodstatic_method111122Traceback (most recent call last): File "/home/homer/workspace/myPython/com/connmiliao.py", line 40, in <module> t.getPr() AttributeError: 'Test' object has no attribute 'getPr'</module> |
示例:@property,@staticmethod,@classmethod
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#!/usr/bin/env python# -*- coding: utf-8 -*-# blog.ithomer.netclass MyClass(object): def __init__(self): print '__init__' self._name = 'blog.ithomer.net' @staticmethod def static_method(): print 'This is a static method!' def test(self): print 'call test' @classmethod def class_method(cls): print 'cls: ',cls print 'cls.name: ',cls.name print 'cls.static_method(): ',cls.static_method() instance = cls() print 'instance.test(): ',instance.test() @property def name(self): return self._name @name.setter def name(self, value): self._name = valueif __name__ == '__main__': MyClass.static_method() MyClass.class_method() mc = MyClass() print mc.name mc.name = 'forum.ithomer.net' print mc.name |
运行结果:
This is a static method!
cls: <class '__main__.myclass'="">
cls.name:
cls.static_method(): This is a static method!
None
__init__
instance.test(): call test
None
__init__
blog.ithomer.net
forum.ithomer.net
python -- @classmethod @staticmethod区别和使用的更多相关文章
- 【python】classmethod & staticmethod 区别
来源:http://blog.csdn.net/carolzhang8406/article/details/6856817 其他参考: http://blog.csdn.net/lovingprin ...
- python中@classmethod @staticmethod区别
Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...
- python中@classmethod @staticmethod区别(转)
pthon中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): print ...
- [转]python中@classmethod @staticmethod区别
Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...
- django classonlymethod 和 python classmethod的区别
--classmethod可以被一个实例调用,classonlyethod只能被类调用 class Kls(object): no_inst = 0 def __init__(self): Kls.n ...
- 基于python中staticmethod和classmethod的区别(详解)
例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object): def foo(self,x): print "executing foo ...
- python @classmethod和@staticmethod区别
python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...
- 模块复习 staticmethod和classmethod的区别
Python中classmethod与staticmethod区别 classmethod:类方法staticmethod:静态方法 在python中,静态方法和类方法都是可以通过类对象和类对象实例访 ...
- python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?
回答背景知识 这些都是装饰器(decorator).装饰器是一种特殊的函数,要么接受函数作为输入参数,并返回一个函数,要么接受一个类作为输入参数,并返回一个类. @标记是语法糖(syntactic s ...
随机推荐
- Class.forName之坑
今天遇到个问题 找不到类,最后发现 Class.forName中要完整的类名
- 结构型--代理模式(Proxy)
一.代理模式是什么? 代理模式属于GOF23设计模式中结构型中的设计模式,通过代理对象来屏蔽(部分或者屏蔽)对真实对象的直接访问,下图为UML图: 在代理模式中组件包括:抽象角色接口.代理角色类.真实 ...
- 深入理解jQuery插件开发总结(一)
由于这篇文章比较长,所以分了四个阶段讲,从简单的入门级到最后到综合级,有些列子和图片都是转载其他博主的,希望对想写插件对同学会有帮助.这里分享个好久之前写的一个jquery插件(网站的功能引导插件,思 ...
- 洛谷P2831 愤怒的小鸟(状压dp)
题意 题目链接 Sol 这题....我样例没过就A了??..算了,就当是样例卡精度吧.. 直接状压dp一下,\(f[sta]\)表示干掉\(sta\)这个集合里面的鸟的最小操作数 转移的时候判断一下一 ...
- JavaScript的重载(通过argument.length)
偶然间在博客园看到的关于js的重载(重载就是一组具有相同名字.不同参数列表,实现不同操作的函数或方法)问题,作为初学者,在看红宝书的时候,记得书中有概念说明js是没有重载的 所以,觉得有必要把这一段 ...
- mockjs
首先还是那句话,进来的GodBoy and GoodGirl 不妨看完再离开. 一个走在路上的前端攻城狮-along 一.mock的由来 mock有“愚弄.欺骗”之意,在前端领域,mock可以理解为 ...
- 新项目放到jenkins步骤
1配置playbook.xml,src和dest需要和运维确认 2项目内config 文件夹下index文件内,build对象内assetsPublicPath属性是否需要更改. playbo ...
- eclipse遇到的异常
1. Widget disposed too early for part com.kompakar.ehealth.ui.emr.mstr.medicaldocumentaudit.Medical ...
- python 数据结构应用
- python mqtt client publish操作
使用Python库paho.mqtt.client 模拟mqtt client 连接broker,publish topic. #-*-coding:utf-8-*- import paho.mqtt ...