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.net class 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_method static_method class_method static_method Test.__init__ class_method static_method 11 11 22 Traceback (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.net class 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 = value if __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 ...
随机推荐
- JUC总览,来自汪文君整理
- linux 软件连接 创建/查看/删除
1.建立软链接 具体用法是:ln -s 源文件 目标文件.源:实际存放文件的位置 当 我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的 ...
- div中嵌套div中使用margin-top失效问题
div中嵌套div中使用margin-top失效问题
- Hibernate 性能优化一对一关联映射
概述: hibernate提供了两种映射一对一关联的方式:按照外键映射和按照主键映射. 下面以员工账号和员工档案为例 ,介绍两种映射方式,并使用这两种映射方式分别完成以下持久化操作: (1)保存员工档 ...
- Maven pom.xml 常用打包配置
<build> <!-- 指定JAVA源文件目录 --> <sourceDirectory>src</sourceDirectory> <!-- ...
- centos install redis
1. 下载 [logan@localhost java]$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz2. 解压 [log ...
- 如何让div覆盖canvas元素
第一步 请让该div和canvas同样处于同一画布,都用position:absolute; 然后设置canvas的z-index="-1",是的,你没看错 然后把要覆盖canva ...
- hiho一下 第一周 最长回文子串
时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这 ...
- opencv 从摄像头中读取视频并保存(c++版)
原文:http://blog.csdn.net/zhongshijunacm/article/details/68947890 OpenCV中的视频操作函数如下表所列: VideoCapture Vi ...
- mysql游标的用法及作用
1当前有三张表A.B.C其中A和B是一对多关系,B和C是一对多关系,现在需要将B中A表的主键存到C中:常规思路就是将B中查询出来然后通过一个update语句来更新C表就可以了,但是B表中有2000多条 ...