python中的定义:

  1. class MyClass:
  2. ...
  3. @classmethod  # classmethod的修饰符
  4. def class_method(cls, arg1, arg2, ...):
  5. ...
  6. @staticmethod  # staticmethod的修饰符
  7. def static_method(arg1, arg2, ...):
  8. ...

@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区别和使用的更多相关文章

  1. 【python】classmethod & staticmethod 区别

    来源:http://blog.csdn.net/carolzhang8406/article/details/6856817 其他参考: http://blog.csdn.net/lovingprin ...

  2. python中@classmethod @staticmethod区别

    Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...

  3. python中@classmethod @staticmethod区别(转)

    pthon中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): print ...

  4. [转]python中@classmethod @staticmethod区别

    Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...

  5. django classonlymethod 和 python classmethod的区别

    --classmethod可以被一个实例调用,classonlyethod只能被类调用 class Kls(object): no_inst = 0 def __init__(self): Kls.n ...

  6. 基于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 ...

  7. python @classmethod和@staticmethod区别

    python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...

  8. 模块复习 staticmethod和classmethod的区别

    Python中classmethod与staticmethod区别 classmethod:类方法staticmethod:静态方法 在python中,静态方法和类方法都是可以通过类对象和类对象实例访 ...

  9. python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?

    回答背景知识 这些都是装饰器(decorator).装饰器是一种特殊的函数,要么接受函数作为输入参数,并返回一个函数,要么接受一个类作为输入参数,并返回一个类. @标记是语法糖(syntactic s ...

随机推荐

  1. dategrid快速录入一行数据的一波操作

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. springboot自定义异常

    SpringBoot自定义异常以及异常处理 在web项目中,我们可能需要给前端返回不同的提示码.例如:401表示没有权限,500代表位置异常,200代表请求成功等.但是这些提示码远远不能满足我们返回给 ...

  3. hdu Anniversary party 树形DP,点带有值。求MAX

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. Tomcat添加用户

    在conf目录下tomcat-users.xml文件里添加如下代码: <role rolename="manager-gui"/> <user username= ...

  5. 使用JS完成注册表单的数据校验

    1.前台校验 防君子不防小人 JavaScript被设计用来向HTML页面添加交互行为 JavaScript是一种脚本语言(脚本语言是一种轻量级的编程语言) JavaScript由数行可执行计算机代码 ...

  6. div 居中方法汇总

    本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 情况一: 父子容器宽高已知 方法一 html <div class= ...

  7. 浅谈App原生开发、混合开发及HTML5开发的优劣

    App混合开发(英文名:Hybrid App),是指在开发一款App产品的时候为了提高效率.节省成本即利用了原生的开发技术还应用了HTML5开发技术,是原生和HTML5技术的混合应用.目前App的开发 ...

  8. Laravel Query Builder 复杂查询案例:子查询实现分区查询 partition by

    案例 案例:Laravel 在文章列表中附带上前10条评论?,在获取文章列表时同时把每个文章的前10条评论一同查询出来. 这是典型分区查询案例,需要根据 comments 表中的 post_id 字段 ...

  9. 最短路径-Dijkstra算法与Floyd算法

    一.最短路径 ①在非网图中,最短路径是指两顶点之间经历的边数最少的路径. AE:1    ADE:2   ADCE:3   ABCE:3 ②在网图中,最短路径是指两顶点之间经历的边上权值之和最短的路径 ...

  10. dbms_randon package

    reference to wbsite:http://zhangzhongjie.iteye.com/blog/1948930#comments DBMS_RANDON PACKAGE: Define ...