【转】Python @classmethod @staticmethod

今天读代码的时候发现Python的class定义中提及了@classmethod修饰符,然后查阅了一些材料一探究竟,先做个总结吧。

在Python中提到 classmethod 就要提到 staticmethod,不是因为二者有什么关系,而是为了让用户区分以便更清楚地写代码。在C++中,我们了解直接通过类名访问的函数称为类的静态函数,即static修饰的函数,可见C++中classmethod和staticmethod是一个概念。

那么python中二者有什么区别呢?先来看下二者如何在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参数中则不需要传递类名,其实这就是二者最大的区别。

二者都可以通过类名或者类实例对象来调用,因为强调的是classmethod和staticmethod,所以在写代码的时候最好使用类名,良好的编程习惯吧。

对于staticmethod就是为了要在类中定义而设置的,一般来说很少这样使用,可以使用模块级(module-level)的函数来替代它。既然要把它定义在类中,想必有作者的考虑。

对于classmethod,可以通过子类来进行重定义。

提到类级别的函数,也顺带提及类级别的变量

  1. class MyClass:
  2. i = 123 # class-level variable
  3. def __init__(self):
  4. self.i = 456 # object-level variable
  5. ...
  6. ...

为了清晰地区分上面两个i,最好的办法就是考虑到python中的一切都是object,所以i=123属于class object的,i=456属于class instance object

原文链接:

http://my.chinaunix.net/space.php?uid=1721137&do=blog&id=274710

【转】Python @classmethod @staticmethod,布布扣,bubuko.com

时间: 2024-08-26 21:05:12

【转】Python @classmethod @staticmethod的相关文章

python -- @classmethod @staticmethod区别和使用

python中的定义: class MyClass: ... @classmethod  # classmethod的修饰符 def class_method(cls, arg1, arg2, ...): ... @staticmethod  # staticmethod的修饰符 def static_method(arg1, arg2, ...): ... @classmethod : 类方法 @staticmethod : 静态方法 类方法和静态方法的调用一样,都是通过类就可以直接调用. 区

Python @classmethod&@staticmethod 区别

转自Stackoverflow@classmethod means: when this method is called, we pass the class as the first argument instead of the instance of that class (as we normally do with methods). This means you can use the class and its properties inside that method rath

python 装饰器语法糖(@classmethod @staticmethod @property @name.)原理剖析和运用场景

引用:http://blog.csdn.net/slvher/article/details/42497781 这篇文章系统的介绍这几者之间的关系和区别.有兴趣的朋友可以到上面的链接查看原文,这里我把原文拷贝如下(如有侵权,通知马上删除) ==================================================================== 在阅读一些开源Python库的源码时,经常会看到在某个类的成员函数前,有类似于@staticmethod或@classme

python @classmethod 的使用场合

python @classmethod 的使用场合 官方的说法: classmethod(function)中文说明:classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下: class C: @classmethod def f(cls, arg1, arg2, ...): ... 看后之后真是一头雾水.说的啥子东西呢??? 自己到国外的论坛看其他的例子和解释,顿时就很明朗. 下面自己用例子来说明. 看下面的定义的一个时间类: class Dat

Python:staticmethod vs classmethod

Being educated under Java background, static method and class method are the same thing. But not so in Python, there is subtle difference: Say function a() is defined in Parent Class, while Sub Class extends Parent Class If function a() has @staticme

python中staticmethod classmethod及普通函数的区别

staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象 (python里光说对象总是容易产生混淆, 因为什么都是对象,包括类,而实际上 类实例对象才是对应静态语言中所谓对象的东西)来调用而已, 不会隐式地传入 任何参数.这个和静态语言中的静态方法比较像. classmethod 是和一个class相关的方法,可以通过类或类实例调用, 并将该class对象(不是class的实例对象)隐式地 当作第一个参数传入. 就这种方法可能会比较奇怪一点,不过只要你搞清楚了pyth

[python学习篇][书籍学习][python standrad library][内建函数]之[all,any,basestring,isinstance,bin,bool,@classmethod,@staticmethod,cmp,enumerate

Python 解释器内置了一些函数,它们总是可用的.这里将它们按字母表顺序列出.     Built-in Functions     abs() divmod() input() open() staticmethod() all() enumerate() int() ord() str() any() eval() isinstance() pow() sum() basestring() execfile() issubclass() print() super() bin() file

python中@classmethod @staticmethod区别

Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): print("executing foo(%s,%s)" % (self, x)) print('self:', self) @classmethod def class_foo(cls, x): print("executing class_foo(%s,%s)" % (cl

python @classmethod 和 @staticmethod区别

首先来看@staticmethod,这个装饰器很好理解,就是让类中的方法变成静态方法,不需要对象实例化就可以直接调用.在此静态方法中无法使用"self"参数: 再看@classmethod.其实和@staticmethod功能类似, 可以简单理解为@staticmethod功能上增加了一个"cls"参数代表本类obj(在类继承中,cls代表子类),可以通过调用cls()对象调用本类中的其他对象(python中所有皆是对象). 如果你对以上两句话还不能理解,请看下面的