博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python @classmethod @staticmethod
阅读量:6860 次
发布时间:2019-06-26

本文共 1350 字,大约阅读时间需要 4 分钟。

hot3.png

 

@classmethod means:

when this method is called, we pass the class as the first argument instead of the instance of that class. This means you can use the class and its properties inside that method rather than a particular instance.

means:

when this method is called, we don't pass an instance of the class to it .This means you can put a function inside a class but you can't access the instance of that class (this is useful when your method does not use the instance).

#!/usr/bin/python#coding:utf-8class Person:    def __init__(self):            print "init"    @staticmethod    def s_sayHello(hello):        print "fun:s_sayhello %s" %hello    @classmethod    def c_introduce(clazz,hello):        clazz.s_sayHello(hello)    def hello(self,hello):        self.s_sayHello(hello)   def main():    print "Person.s_sayHello:"    Person.s_sayHello("Person:s_sayHello!")    print "Person.c_introduce"    Person.c_introduce("Person:c_introduce!");print "*"* 20;p=Person()    p.s_sayHello("p:s_sayHello!")    p.c_introduce("p:c_introduce!")    p.hello("p:hello!")if __name__=='__main__':    main()输出Person.s_sayHello:fun:s_sayhello Person:s_sayHello!Person.c_introducefun:s_sayhello Person:c_introduce!********************initfun:s_sayhello p:s_sayHello!fun:s_sayhello p:c_introduce!fun:s_sayhello p:hello!

 

转载于:https://my.oschina.net/u/347414/blog/699786

你可能感兴趣的文章
利用脚本在Virtualbox中部署fuel Openstack
查看>>
Jetty - 嵌入式运行Servlet
查看>>
同一个服务器安装两个tomcat
查看>>
链表的插入排序 Insertion Sort List
查看>>
我的友情链接
查看>>
曹丽丽:与百度互动必听--百度站长平台新动向
查看>>
BUG:由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值...
查看>>
fastDFS安装和使用
查看>>
Linux增加硬盘并挂载到VLM逻辑卷
查看>>
VMware Horizon View 7: Instant Clone Desktop Pool [Part 8]
查看>>
直接使用AD验证SSL ×××用户并且使用AD给用户添加banner
查看>>
学习从晚上十点开始
查看>>
怎样向azkaban贡献代码
查看>>
log4j日志写入redis扩展实现(log4j-redis-appender)
查看>>
我的友情链接
查看>>
Cobbler 安装及应用
查看>>
分享7个超实用的Emmet(zen coding)HTML代码使用技巧
查看>>
在windows server 2012 R2 hyper-v 上布署 Citrix XenDesktop 7.6 (前言)
查看>>
编译Linux内核4.6.3使其支持NTFS文件系统
查看>>
统计php程序运行时间
查看>>