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 ...
随机推荐
- linux修改用户名和密码
linux修改用户名和密码 修改root密码:sudo passwd root 修改用户密码(如hadoop) sudo passwd hadoop 修改主机名:sudo vi /etc/hostna ...
- 一、快速构建Springboot应用
1.基本概念 Spring的出现对于企业级应用来说是一个福音,它让企业级应用开发更加地简单.但是随着Spring的不断发展,它也慢慢变得越来越重.即使apache出品的maven工具能够使得项目创建. ...
- IntelliJ IDEA16 热部署,解决每次修改java文件就得重启tomcat的问题
这样就可以了....
- [国家集训队2012]middle(陈立杰)
我是萌萌的传送门 我是另一个萌萌的传送门 脑残错误毁一下午…… 其实题解早就烂大街了,然而很久之前我只知道是二分答案+主席树却想不出来这俩玩意儿怎么一块儿用的……今天又翻了几篇题解才恍然大悟,是把权值 ...
- MySQL库表详细操作
昨天我们初始了MySQL,今天我们先从库表方面详细说一下具体操作 一.库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset utf8; 1.2 数据库命名规则 ...
- javascript多浏览器的兼容
一.document.formName.item(”itemName”) 问题 问题说明:IE下,可以使用 document.formName.item(”itemName”) 或 document. ...
- C语言——单链表初始化、求表长、读表元素、插入元素
头文件Linear.h // 单链表的类型定义 typedef struct node { int data; // 数据域 struct node *next; // 指针域 }Node, *Lin ...
- 使用dojo.connect()添加事件的注意事项
使用dojo.connect()添加事件处理器是很方便的,不用再考虑跨浏览器的问题了.但要想正确地使用这个方法,仍然要注意几个问题: 1.用dojo.byId()获取的是dom元素,而用 ...
- 谈谈CSS性能
CSS性能优化 1.衡量属性和布局的消耗代价: 2.探索W3C的性能优化新规范: 3.用测试数据判断优化策略. 慎重选择高消耗的样式 1.box-shadows; 2.border-radius; 3 ...
- 使用Mist部署Contract到Rinkeby以太坊网络
本文使用MyEthWallet新建一个账号,并导入到Mist中,然后部署Contract到Rinkeby网络使用MyEthWallet新建账号的好处是除了JSON文件之外,还能得到一张它生成的pdf( ...