Python 不仅是一种流行的脚本语言,还支持面向对象编程范式。类描述数据并提供操作数据的方法,所有这些都包含在一个对象中。此外,类将具体的实现细节与数据的抽象表示分离开来,从而实现了抽象。
使用类的代码通常更容易阅读、理解和维护。
Section 38.1: 类的简介
类作为一个模板,定义了特定对象的基本特征。下面是一个例子:
class Person(object):
"""A simple class.""" # docstring
species = "Homo Sapiens" # class attribute
def __init__(self, name): # special method
"""This is the initializer. It's a special
method (see below).
"""
self.name = name # instance attribute
def __str__(self): # special method
"""This method is run when Python tries
to cast the object to a string. Return
this string when using print(), etc.
"""
return self.name
def rename(self, renamed): # regular method
"""Reassign and print the name attribute."""
self.name = renamed
print("Now my name is {}".format(self.name))
看上面的例子有几点需要注意。
- 类由属性(数据)和方法(功能)组成。
- 属性和方法被简单定义为普通变量和函数。
- 正如相应的文档中指出的,
__init__()方法称为初始化器。它相当于其他面向对象语言中的构造函数,是创建一个新对象或类的新实例时首先运行的方法。 - 首先定义适用于整个类的属性,称为类属性。
- 适用于类的特定实例(对象)的属性称为实例属性。它们通常在
__init__()中定义;这不是必须的,但我们建议这样做(因为在__init__()之外定义的属性有可能在定义之前就被访问)。 - 类定义中包含的每个方法都会将相关对象作为第一个参数传递。这个参数使用了
self这个词(使用self实际上是约定俗成的,因为self这个词在 Python 中没有固有的含义,但这是 Python 最受尊重的约定之一,您应该始终遵循它)。 - 习惯于用其他语言进行面向对象编程的人可能会对一些事情感到惊讶。首先,Python 没有私有元素的真正概念,因此默认情况下,一切都模仿 C++/Java public 关键字的行为。有关详细信息,请参阅本页的 “私有类成员 ”示例。
- 该类的某些方法具有以下形式:
__functionname__(self, other_stuff)。所有这些方法被称为 “魔法方法”,是 Python 类的重要组成部分。例如,Python 中的运算符重载就是用魔法方法实现的。有关详细信息,请参阅相关文档。
现在,让我们为 Person 类创建几个实例!
>>> # Instances
>>> kelly = Person("Kelly")
>>> joseph = Person("Joseph")
>>> john_doe = Person("John Doe")
目前,我们有三个 Person 对象:kelly、joseph 和 john_doe。
我们可以使用点运算符 。请再次注意类属性和实例属性之间的区别:
>>> # Attributes
>>> kelly.species
'Homo Sapiens'
>>> john_doe.species
'Homo Sapiens'
>>> joseph.species
'Homo Sapiens'
>>> kelly.name
'Kelly'
>>> joseph.name
'Joseph'
我们可以使用同样的点操作符 .来执行类的方法:
>>> # Methods
>>> john_doe.__str__()
'John Doe'
>>> print(john_doe)
'John Doe'
>>> john_doe.rename("John")
'Now my name is John'

Section 38.2: 绑定、非绑定和静态方法
Python 3 中删除了绑定和非绑定方法的概念。在 Python 3 中,当您在类中声明一个方法时,会使用 def 关键字,从而创建一个函数对象。这是一个常规函数,周围的类作为它的命名空间。在下面的例子中,我们在类 A 中声明方法 f,它就变成了函数 A.f:
class A(object):
def f(self, x):
return 2 * x
A.f
# <function A.f at ...>

在 Python 3 中,方法被识别为函数,而在 Python 2 中,这种区别被保留了下来。
import inspect
inspect.isfunction(A.f)
# True
inspect.ismethod(A.f)
# False

在 Python 的两个版本中,只要传递一个类 A 的实例作为第一个参数,就可以直接调用函数/方法 A.f。
A.f(1, 7)
# 输出: 14
a = A()
A.f(a, 20)
# 输出: 40

现在假设 a 是类 A 的一个实例,那么 a.f 是什么呢?直观地说,这应该是类 A 的同一个方法 f,只是它应该以某种方式 “知道 ”它被应用于对象 a - 在 Python 中,这被称为绑定到 a 的方法。
具体细节如下:写 a.f 会调用 a 的神奇的 __getattribute__方法,它首先检查 a 是否有名为 f 的属性(没有),然后检查类 A 是否包含有这样名字的方法(有),并创建一个新的 method 类型的对象 m,它在 m.__func__ 中有对原始 A.f 的引用,在 m.__self__ 中有对对象 a 的引用。当这个对象作为函数调用时,它只需执行以下操作: m(...) => m.__func__(m.__self__,...)。因此,这个对象被称为 绑定方法,因为当调用它时,它知道要提供绑定的对象作为第一个参数。(在 Python 2 和 Python 3 中,这些工作方式是一样的)。
a = A()
a.f
# <bound method A.f of <__main__.A object at ...>>
a.f(2)
# 4
# 注意:每次调用绑定方法对象 a.f 时,都会重新创建该对象:
a.f is a.f # False
# 为了优化性能,可以将绑定的方法存储在对象的 __dict__ 中,在这种情况下,方法对象将保持固定:
a.f = a.f
a.f is a.f # True

最后,Python 有类方法和静态方法--特殊类型的方法。类方法的工作方式与普通方法相同,只是在对象上调用时,它们绑定的是对象的类而不是对象。因此,m.__self__ = type(a)。当你调用这种绑定方法时,它会传递 a 的类作为第一个参数。静态方法更简单:它们根本不绑定任何东西,只是简单地返回底层函数,而不进行任何转换。
class D(object):
multiplier = 2
@classmethod
def f(cls, x):
return cls.multiplier * x
@staticmethod
def g(name):
print("Hello, %s" % name)
D.f
# <bound method type.f of <class '__main__.D'>>
D.f(12)
# 24
D.g
# <function D.g at ...>
D.g("world")
# Hello, world

请注意,即使在实例上访问类方法,类方法也与类绑定:
d = D()
d.multiplier = 1337
(D.multiplier, d.multiplier)
# (2, 1337)
d.f
# <bound method D.f of <class '__main__.D'>>
d.f(10)
# 20

值得注意的是,在最底层,函数、方法、静态方法等实际上是调用 __get__、__set__ 和可选 __del__特殊方法的描述符。有关 classmethods 和 staticmethods 的更多详情:
Section 38.3: 基本继承
Python 中的继承基于 Java、C++ 等其他面向对象语言中使用的类似思想。一个新类可以从一个现有类派生出来,如下所示。
class BaseClass(object):
pass
class DerivedClass(BaseClass):
pass
BaseClass 是已经存在的(父)类,而 DerivedClass 是继承(或子类化)了 BaseClass 属性的新(子)类。注:从 Python 2.2 开始,所有类都隐式地继承自对象类,对象类是所有内置类型的基类。
我们在下面的示例中定义了一个父类 Rectangle,它隐式地继承自 object:
class Rectangle():
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
def perimeter(self):
return 2 * (self.w + self.h)
矩形类可以作为定义正方形类的基类,因为正方形是矩形的特例。
class Square(Rectangle):
def __init__(self, s):
# call parent constructor, w and h are both s
super(Square, self).__init__(s, s)
self.s = s
Square 类将自动继承 Rectangle 类以及对象类的所有属性。super()用于调用 Rectangle 类的 __init__() 方法,本质上是调用基类的任何重载方法。
注意:在 Python 3 中,super() 不需要参数。
派生类对象可以访问和修改基类的属性:
r.area()
# Output: 12
r.perimeter()
# Output: 14
s.area()
# Output: 4
s.perimeter()
# Output: 8
与继承相关的内置函数
issubclass(DerivedClass, BaseClass):如果 DerivedClass 是 BaseClass 的子类,则返回 True
isinstance(s, Class):如果 s 是 Class 或 Class 的任何派生类的实例,则返回 True
# subclass check
issubclass(Square, Rectangle)
# Output: True
# instantiate
r = Rectangle(3, 4)
s = Square(2)
isinstance(r, Rectangle)
# Output: True
isinstance(r, Square)
# Output: False
# A rectangle is not a square
isinstance(s, Rectangle)
# Output: True
# A square is a rectangle
isinstance(s, Square)
# Output: True

Section 38.4: 猴子补丁
在这种情况下,"猴子补丁" 指的是在类被定义后为其添加一个新变量或方法。例如,我们把类 A 定义为
class A(object):
def __init__(self, num):
self.num = num
def __add__(self, other):
return A(self.num + other.num)
但现在我们想在代码后面添加另一个函数。假设这个函数如下
def get_num(self):
return self.num
但我们如何将其添加为 A 中的方法呢?很简单,我们只需在 A 中加入赋值语句即可。
A.get_num = get_num
为什么会这样呢?因为函数和其他对象一样是对象,而方法是属于类的函数。
函数 get_num 应可用于所有现有的(已创建的)以及 A 的新实例。
类(或其子类)的所有实例自动使用这些新增功能。例如
foo = A(42)
A.get_num = get_num
bar = A(6);
foo.get_num() # 42
bar.get_num() # 6

需要注意的是,与其他一些语言不同的是,这种方法不适用于某些内置类型,而且也不被认为是好的风格。