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 ...
随机推荐
- spring AOP Capability and Goals(面向方面编程功能和目标归纳)
原官方文档链接: https://docs.spring.io/spring/docs/5.1.6.RELEASE/spring-framework-reference/core.html#aop-i ...
- eclipse切换workspace后配置问题
正常情况下如果切换了eclipse的workspace后,需要重新配置eclipse,但是可以将原工作目录中的.metadata/.plugins/org.eclipse.core.runtime拷贝 ...
- 域名解析成功后,怎样访问服务器上Eclipse中的Web工程
右击工程选择Export-->选择Web文件夹下的WAR file-->Destination下选择文件存放的地址-->Finish就可以获得WAR文件了然后将WAR文件放到tomc ...
- 键盘录入(Java)
键盘录入(Java): 1.导包 格式 import java.util.Scanner; 位置 在class上面 2.创建键盘录入对象 格式 Scanner sc = new Scanner(Sys ...
- HttpContext.Current.Request.RawUrl是什么意思?
原始 URL 定义为 URL 中域信息之后的部分.在 URL 字符串 http://www.contoso.com/articles/recent.aspx 中,原始 URL 为/articles/r ...
- Java并发编程:volatile关键字解析(学习总结-海子)
博文地址:Java并发编程:volatile关键字解析
- git自动更新网站代码
1.实现过程在linux上安装git服务.创建源版本库.从源版本库克隆得到网站目录,然后利用git中的hooks机制,在git push推送代码到源版本库的时候,触发编写的shell脚本,更新网站目录 ...
- 关于建立Android工程R文件丢失的问题
今天开始学习Android了,好久没打开eclipse,建立Android工程老是报错,于是手残的把appcompat-v7给删了,然后建立工程以后重新出来的appcompat-v7有个小叉号,百度了 ...
- Ubuntu上如何搭建Android开发环境
1.以下是开始Android应用程序编程之前需要的软件列表: a.Java JDK5 及以后版本 b.Java运行环境 c.Android Studio 2.安装Android Studio: x64 ...
- java实现哈弗曼树和哈夫曼树压缩
本篇博文将介绍什么是哈夫曼树,并且如何在java语言中构建一棵哈夫曼树,怎么利用哈夫曼树实现对文件的压缩和解压.首先,先来了解下什么哈夫曼树. 一.哈夫曼树 哈夫曼树属于二叉树,即树的结点最多拥有2个 ...