classmethod 的使用方法
来源:互联网 发布:mac极点五笔皮肤包 编辑:程序博客网 时间:2024/05/18 02:05
转载自http://xuqingkuang.is-programmer.com/2009/3/6/python-technique-classmethod-decorator.7404.html
通常情况下,如果我们要使用一个类的方法,那我们只能将一个类实体化成一个对象,进而调用对象使用方法。
比如:
class Hello(object):
def __init__:
...
def print_hello(self):
print "Hello"
def __init__:
...
def print_hello(self):
print "Hello"
要用 print_hello() 就得:
hlo = Hello()
hlo.print_hello()
Hello
hlo.print_hello()
Hello
如果用了 @classmethod 就简单了。
class Hello(object):
def __init__:
...
@classmethod
def print_hello(cls):
print "Hello"
def __init__:
...
@classmethod
def print_hello(cls):
print "Hello"
要用的话直接:
Hello.print_hello()
Hello
Hello
注意:@classmethod 仅仅适用于单独的,与类本身的数据结构无关函数,其实用了它的函数,与使用普通函数无异,甚至不能在参数里加入 self,如果要在其中使用类的数据结构,仍然需要将类实例化一次才可以,所以要小心使用。
0 0
- classmethod 的使用方法
- python的staticmethod、classmethod
- python 的@classmethod &&@staticmethod
- @classmethod
- @classmethod
- python的staticmethod和classmethod
- @staticmethod和@classmethod的用法
- staticmethod和classmethod的区别
- python @classmethod 的使用场合
- python的@classmethod和@staticmethod
- Python @staticmethod 和 @classmethod 的异同
- 理解python的staticmethod与classmethod实现
- python的cls,self,classmethod,staticmethod
- python的cls,self,classmethod,staticmethod
- 【Python】Staticmethod和classmethod的区别
- 理解python的staticmethod与classmethod实现
- Python中的staticmethod和classmethod 的异同
- python中staticmethod和classmethod的区别
- Java 并发和多线程向导(1)
- box-sizing border-box 的理解
- fopen与open的区别
- ArcGIS10.1和ArcGIS Server10.1安装和破解:
- 常见OOM
- classmethod 的使用方法
- web视频播放组件 flowplayer介绍
- deepin 死机了怎么办
- DP讲解
- custom pygtk widgets
- “failed to update unity web player”失败的问题:
- SDE与oracle表分区(一)
- java +mysql 中文乱码
- poj3349 Snowflake Snow Snowflakes