@

  首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行。只可以对模块或者类定义的函数进行修饰,不允许修饰一个类。一个修饰也就是一个函数,它将被修饰的函数作为参数,并返回修饰后同名函数的调用。

#-*- coding:utf-8 -×-

def fun(f):
print 'AAAA'
return f('BBBB') @fun
def fun_1(s):
print s def fun_2(s):
print s
fun(fun_2)

输出:

AAAA
BBBB
AAAA
BBBB

@staticmethod和@classmethod

  python其实有3个方法,静态方法、类方法和实例方法如下:

#-*- coding:utf-8 -*-
def fun(x):
print "fun for %s"%x class A(object):
def fun(self,x):
print "fun for (%s,%s)"%(self,x) @classmethod
def class_fun(cls,x):
print "fun for (%s,%s)" % (cls, x) @staticmethod
def static_fun(x):
print "fun for %s" % x fun(1)
a=A()
a.fun(1)
a.class_fun(1)
a.static_fun(1) A().fun(1) #A.fun(1)是非法的
A.class_fun(1)
A.static_fun(1)

输出:

fun for 1
fun for (<__main__.A object at 0x7f40fd9c5ed0>,1)
fun for (<class '__main__.A'>,1)
fun for 1
fun for (<__main__.A object at 0x7f40fd9d4150>,1)
fun for (<class '__main__.A'>,1)
fun for 1

  这里先理解一下参数中的self和cls,这里的self和cls是对类或者实例的绑定。对于一般的函数来说我们可以直接调用,例如‘fun(1)’,这也是最常见的,它的工作和任何的类、实例没有关系,但类里面定义的方法都需要绑定这个实例,比如‘fun(self,x)’,之所以这样做,是因为实例方法的调用离不开实例,我们需要把实例自己传给参数,调用的时候比如‘a.fun(1)’(其实是fun(a,1)),类的方法也是一样的,只不过它传递的是类而不是实例,比如‘A.class_fun(1)’,注意这里的self和cls可以换成别的参数,但是python约定就是这样。

  实例方法  类方法  静态方法

  a=A()       a.fun(x)  a.class_fun(x

  A              不可用      A.class_fun

python中@staticmethod与@classmethod的更多相关文章

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

  2. python中@staticmethod、@classmethod和实例方法

    1.形式上的异同点: 在形式上,Python中:实例方法必须有self,类方法用@classmethod装饰必须有cls,静态方法用@staticmethod装饰不必加cls或self,如下代码所示: ...

  3. python中 staticmethod与classmethod

    原文地址https://blog.csdn.net/youngbit007/article/details/68957848 原文地址https://blog.csdn.net/weixin_3565 ...

  4. 面试题:python 中 staticmethod 和 classmethod有什么区别

    面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...

  5. python 中 staticmethod 和 classmethod有什么区别

    面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...

  6. python中 staticmethod与classmethod区别

    staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...

  7. Python中@staticmethod和@classmethod的作用和区别

    简单介绍一下两者的区别: 对于一般的函数test(x),它跟类和类的实例没有任何关系,直接调用test(x)即可 #!/usr/bin/python # -*- coding:utf-8 -*- de ...

  8. (转)关于python3中staticmethod(静态方法)classmethod(类方法)实例方法的联系和区别

    原文:http://dmcoders.com/2017/08/30/pythonclass/ https://zhuanlan.zhihu.com/p/28010894------正确理解Python ...

  9. python 之@staticmethod和@classmethod

    在python中,要调用一个类中的方法,一般的操作步骤如下: 1.实例化此类 2.调用此类中的方法 而@staticmethod和@classmethod则打破了这种引用方式,可以在不实例化类的情况下 ...

随机推荐

  1. Mybatis-Plus 实战完整学习笔记(六)------select测试一

    查询方法(3.0.3) 1.查询一个员工的数据 @Test public void selectMethod() throws SQLException { // 根据ID获取一个对象的数据 Empl ...

  2. CPU load高而使用率低的问题分析

    最近服务器上出现了一个很诡异的问题,症状如下图所示: 查看进程发现: 如上图所示,非常多的df -h进程没有退出.于是手工kill掉这些 df -h进程.cpu load恢复正常. 至于为什么会有这么 ...

  3. AngularJS实战之filter的使用一

    一.格式化数字为货币格式. <div>{{money|currency:"$"}}</div> <div>{{money|currency:&q ...

  4. silverlight 定时器 System.Windows.Threading.DispatcherTimer

    声明 System.Windows.Threading.DispatcherTimer _MessageControler; //刷新 _MessageControler = new System.W ...

  5. 20169207《Linux内核原理及分析》第十三周作业

    第一周作业::对Linux的基本知识进行了了解,并对基本操作进行熟悉和应用. 第二周作业::了解了冯诺依曼体系结构.各种寄存器的功能和汇编指令的作用和功能. 第三周作业::这周主要了解了Linux系统 ...

  6. (暴力 记录)Camellia的难题 -- zzuli -- 1784

    http://acm.zzuli.edu.cn/problem.php?id=1784 Camellia的难题 Time Limit: 2 Sec  Memory Limit: 128 MBSubmi ...

  7. java 判断手机号码和邮箱的正则表达式

    很多场合会用到判断输入框输入的是否为手机或者邮箱,下面是这个正则表达式: Pattern  patternMailBox  = Pattern .compile( "^([a-zA-Z0-9 ...

  8. Concurrency Programming Guide 并发设计指引(二)

    以下翻译是本人通过谷歌工具进行翻译,并进行修正后的结果,希望能对大家有所帮助.如果您发现翻译的不正确不合适的地方,希望您能够发表评论指正,谢谢.转载请注明出处. Concurrency and App ...

  9. spring boot 整合 mybatis 以及原理

    同上一篇文章一样,spring boot 整合 mybatis过程中没有看见SqlSessionFactory,sqlsession(sqlsessionTemplate),就连在spring框架整合 ...

  10. FNDLOAD使用大全

    FNDLOAD使用大全   Syntax FNDLOAD [username/password] 0 Y [mode] [configuration file] [target data file] ...