python @classmethod 和 @staticmethod区别

首先来看@staticmethod,这个装饰器很好理解,就是让类中的方法变成静态方法,不需要对象实例化就可以直接调用。在此静态方法中无法使用"self"参数;

再看@classmethod。其实和@staticmethod功能类似, 可以简单理解为@staticmethod功能上增加了一个"cls"参数代表本类obj(在类继承中,cls代表子类),可以通过调用cls()对象调用本类中的其他对象(python中所有皆是对象)。

如果你对以上两句话还不能理解,请看下面的代码,就理解了。

 1 class Test(object):
 2     tag=1
 3
 4     def normal_method(self):
 5         print ‘normal method‘
 6
 7     @classmethod
 8     def class_method(cls):
 9         print ‘class method, tag is %s‘ % cls().tag
10         cls().normal_method()
11
12     @staticmethod
13     def static_method():
14         print ‘static method, tag is %s‘ % Test.tag
15         # print ‘static method,tag is %s‘% Test.class_method()

运行结果>>>

>>> Test.static_method()
static method, tag is 1
>>> Test.class_method()
class method, tag is 1
normal method
时间: 2024-07-30 14:55:16

python @classmethod 和 @staticmethod区别的相关文章

Python中classmethod与staticmethod区别

classmethod:类方法staticmethod:静态方法 在python中,静态方法和类方法都是可以通过类对象和类对象实例访问.但是区别是: @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法. 类方法的第一个参数cls,而实例方法的第一个参数是self,表示该类的一个实例. 普通对象方法至少需要一个self参数,代表类对象实例 类方法有类变量cls传入,从而可以用cls做一些相关的处理.并且有子类继承时,调用该类方法时,传入的类变

Python的classmethod和staticmethod区别

静态方法(staticmethod) 类方法(classmethod) 静态方法和类方法都可以通过类名.方法名或者实例.方法访问. #-*- coding:utf8 -*- class A(object): def instance_method(self,x): print "instance_method (%s,%s)" % (self,x) @classmethod def class_method(cls,x): #类方法的调用使用类本身作为其隐含的参数 #调用者本身并不需要

classmethod和staticmethod区别

实例方法:在类中,定义的方法,这个方法的第一个参数默认是实例对象,一般习惯使用self 类方法:在类中,定义的方法,这个方法的第一个参数默认是类对象,一般习惯用cls表示,用@classmethod装饰器装饰 静态方法:在类中定义的方法,这个方法的参数没有要求,用@staticmethod装饰器装饰 实例方法只能被实例(对象)调用 类方法和静态方法可以被类或者实例调用 class Foo(object): # 实例方法,第一个参数必须是实例对象.一般习惯用self. def instance_m

classmethod和staticmethod区别(转载)

主要classmethod是被类直接调用使用 statifcmethod是在类内部访问时候并且是能被类直接调用时候使用 原文链接地址:click

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

今天读代码的时候发现Python的class定义中提及了@classmethod修饰符,然后查阅了一些材料一探究竟,先做个总结吧. 在Python中提到 classmethod 就要提到 staticmethod,不是因为二者有什么关系,而是为了让用户区分以便更清楚地写代码.在C++中,我们了解直接通过类名访问的函数称为类的静态函数,即static修饰的函数,可见C++中classmethod和staticmethod是一个概念. 那么python中二者有什么区别呢?先来看下二者如何在pytho

python的@classmethod和@staticmethod

本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象编程中,类中定义的方法可以是@classmethod 装饰的类方法,也可以是@staticmethod 装饰的静态方法,用的最多的还是不带装饰器的实例方法.为方便,在下文中用@classmethod装饰的类方法将直接用@classmethod来表述,@staticmethod同理,望读者在阅读时自行

python中classmethod与staticmethod的差异及应用

类中三种函数的应用 #!/usr/bin/env python # -*- coding: utf-8 -*- class TClassStatic(object): def __init__(self, data): self.data = data def printself(*arg): # for item in arg: # print item.data print("printself: ", arg) @staticmethod def smethod(*arg): p

Python中@property和@classmethod和@staticmethod

前戏 首先,先要弄清楚一个类里面的,各个组成部分都应该怎么称呼. - 注:可能叫法会不太一样. 关于@property 顾名思义:它的意思为'属性'. 作用: 1:使用它你将会把类方法,变为类属性.并且是只读属性. 2:它会重新实现getter和setter方法. 看代码: class Person: def __init__(self,first_name,last_name): self.first_name = first_name self.last_name = last_name @