您的位置:首页 > 其它

@classmethod 和 @staticmethod 方法

2020-08-21 17:54 51 查看

成员方法(实例方法)、静态方法和类方法

实例方法

类中最常用的方法是实例方法, 即通过通过实例作为第一个参数的方法。
举个例子,一个基本的实例方法就向下面这个:

human_count = 0

def __init__(self, name):
self.name = name
Human.human_count += 1

def print_name(self):
print("My name is: ", self.name)

h = Human("Adam")
h.print_name()

运行结果为:

My name is: Adam

这里的 print_name 方法就是个实例方法。该类方法的第一个参数为调用其的实例对象。传入实例对象作为参数是 python 帮我们自动完成的。

类方法和静态方法的引入

在这里向说明两个问题。首先是 python 的机制问题。其次是python 没法像 c++ 那样进行方法的重载(接受的参数个数或类型不同就可以重载一个方法,即用同一个方法名)的问题。为了更好的说明这两个问题,我们下面做两个实验:

实验1:

class Human(object):
human_count = 0

def __init__(self, name):
self.name = name
Human.human_count += 1

def print_name(self):
print("My name is: ", self.name)

def print_hello():
print("Hello, nice world!")

h = Human("Adam")
h.print_name()
h.print_hello()

在实验1中我们写了一个print_hello方法,其不接受任何参数。当我们在实例化一个 Human 对象 h,并且想通过 h 来调用 print_hello 这个方法。但是我们发现,上述代码的输出为:

TypeError                                 Traceback (most recent call last)
<ipython-input-98-8b96112221b9> in <module>()
15 h = Human("Adam")
16 h.print_name()
---> 17 h.print_hello()

TypeError: print_hello() takes no arguments (1 given)

我们发现提示信息告诉我们,print_hello()函数是不需要参数的(这和咱们写这个函数的初衷是一样的),但是该方法却被传入了一个参数,因此报错。从这里我们就知道了 python 的机制。凡是通过类实例调用的方法默认给方法传入了一个参数,也就是该实例本身。也就是之前咱们写实例方法时,每个方法的第一个参数 self。

实验2:

class Human(object):
human_count = 0

def __init__(self, name):
self.name = name
Human.human_count += 1

def print_name(self):
print("My name is: ", self.name)

def print_name(self, a):
print("This is the other print_name method with parameter a: ", a)

h = Human("Adam")
h.print_name()
h.print_name(11)

按照我们的思路,我们是想按照 c++ 重载函数的思想来写一个同名的 print_name 方法,除了实例本身外还接受一个参数 a。然后分别用实例 h 调用两个函数。
输出结果:

TypeError                                 Traceback (most recent call last)
<ipython-input-99-ef128668b1a3> in <module>()
14
15 h = Human("Adam")
---> 16 h.print_name()
17 h.print_name(11)

TypeError: print_name() takes exactly 2 arguments (1 given)

我们发现,输出结果和我们的预想不一样。发现后面写的方法 print_name(self, a) 已经把之前的同名方法覆盖。也就是说前面的那个方法不存在了。此时,类中的实例方法 print_name 只剩下接受两个参数的那个。也就是只剩下 print_name(self, a) 这个方法。因此报错信息说 print_name() 方法需要两个参数,而我们只给了一个(即实例本身)。
通过上述两个实验,我们说明白了 python 调用类内函数的机制以及无法重载函数的问题。这里面特别是因为我们的 python 机制存在,使得我们需要想办法使得一些不需要实力本身作为参数的方法得以创建和正常使用。

类方法

当我们需要和类直接进行交互,而不需要和实例进行交互时。类方法就是最好的选择。但是之前所介绍的 python 机制问题,我们知道我们通过实例调用方法时都会默认传入实例本身作为第一个参数。而如今,类方法不需要和实例进行交互,自然也就不需要传入实例本身。再则,类方法需要和类进行交互,所以如果有种方法能够在每次调用时都传入类对象就好了。而这个方法就是使用 @classmethod 来装饰我们的方法。如下代码:

class Human(object):
human_count = 0

def __init__(self, name):
self.name = name
Human.human_count += 1

def print_name(self):
print("My name is: ", self.name)

@classmethod
def number(cls):
print("The number of people: ", cls.human_count)

h = Human("Adam")
h.print_name()
h.number()
Human.number()

代码输出为:

My name is:  Adam
The number of people:  1
The number of people:  1

经过 @classmethod 装饰后,我们可以直接通过实例来调用类方法,也可以通过类名来调用。而类方法的第一个参数为类对象本身(按照习惯或约定一般写为 cls,代表 class_object 本身)。这样我们就能在类方法中,轻松和类进行交互:访问类本身的属性以及类本身的方法。
我们还可以利用这个特性轻松通过类方法来创建实例,而这个功能就类似于 c++ 中的重载(从外部看起来像是重载了构造函数):

class Human(object):
human_count = 0

def __init__(self, name):
self.name = name
Human.human_count += 1

def print_name(self):
print("My name is: ", self.name)

@classmethod
def number(cls):
print("The number of people: ", cls.human_count)

@classmethod
def through_list(cls, lst):
new_member = list()
for i in range(len(lst)):
new_member.append(cls(lst[i]))
return new_member

h = Human("Adam")
h.print_name()
h.number()
Human.number()
lst = ["Lucy", "Stack", "Lala"]
human_lst = h.through_list(lst)
for i in range(len(human_lst)):
print(human_lst[i].name)

输出结果:

My name is:  Adam
The number of people:  1
The number of people:  1Lucy
Stack
Lala

静态方法

我们经常会遇到一些这样的情况:我们需要一些方法。这些方法和类相关,但是又不需要类和实例中的任何信息、属性等等。如果把这些方法写到类外面,这样就把和类相关的代码分散到类外,使得之后对于代码的理解和维护都是巨大的障碍。而静态方法就是用来解决这一类问题的。举个例子,比如我们现在要设置一个环境变量 RESET。还要写一个方法,这个方法将检测 RESET 变量的值。当变量值r为真时我们需要打印一句话:“The reset is on!”。这个方法和类相关,但又不需要访问任何和类或者实例相关的属性。所以我们希望把它绑定在类内,而不是在类外写一个这样的方法,然后调用。怎么办呢?直接上代码:

RESET = True

class Human(object):
human_count = 0

def __init__(self, name):
self.name = name
Human.human_count += 1

def print_name(self):
print("My name is: ", self.name)

@staticmethod
def whether_reset():
if RESET:
print("The reset is on!")

h = Human("Adam")
h.whether_reset()

运行结果:

The reset is on!

转载自 链接:https://www.jianshu.com/p/ce117e8b1c61

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐