Home  >  Article  >  Backend Development  >  Detailed explanation of classes, inheritance and polymorphism in Python

Detailed explanation of classes, inheritance and polymorphism in Python

不言
不言Original
2018-05-02 15:36:551219browse

This article explains the definition and usage of Python classes, inheritance and polymorphism in detail through examples. It is very practical. Friends in need can refer to the

Definition of classes

If you want to define a class Point to represent a two-dimensional coordinate point:

# point.py
class Point:
  def __init__(self, x=0, y=0):
    self.x, self.y = x, y

The most basic one is the __init__ method, which is equivalent to Constructor in C/Java. Methods with double underscore __ are special methods. In addition to __init__, there are many more, which will be introduced later.

The parameter self is equivalent to C's this, which represents the current instance. All methods have this parameter, but it does not need to be specified when calling.

>>> from point import *
>>> p = Point(10, 10) # __init__ 被调用
>>> type(p)
<class &#39;point.Point&#39;>
>>> p.x, p.y
(10, 10)

Almost all special methods (including __init__) are called implicitly (not called directly).

For Python, where everything is an object, the class itself is of course also an object:

>>> type(Point)
<class &#39;type&#39;>
>>> dir(Point)
[&#39;__class__&#39;, &#39;__delattr__&#39;, &#39;__dict__&#39;, ..., &#39;__init__&#39;, ...]
>>> Point.__class__
<class &#39;type&#39;>

Point is an instance of type, which is the same as p It's one thing to be an instance of Point.

Now add method set:

class Point:
  ...
  def set(self, x, y):
    self.x, self.y = x, y

>>> p = Point(10, 10)
>>> p.set(0, 0)
>>> p.x, p.y
(0, 0)

p.set(... ) is actually just a syntactic sugar. You can also write it as Point.set(p, ...), so that you can clearly see that p is the self parameter:

>>> Point.set(p, 0, 0)
>>> p.x, p.y
(0, 0)

It is worth noting that self is not a keyword and can even be replaced by other names, such as this:

class Point:
  ...
  def set(this, x, y):
    this.x, this.y = x, y

The difference from C is, " "Member variable" must be prefixed with self., otherwise it will become an attribute of the class (equivalent to a C static member) rather than an attribute of the object.

Access control

Python does not have access control such as public / protected / private. If you must express "private", it is customary to add a double underscore prefix.

class Point:
  def __init__(self, x=0, y=0):
    self.__x, self.__y = x, y

  def set(self, x, y):
    self.__x, self.__y = x, y

  def __f(self):
    pass

__x, __y and __f are equivalent to private:

>>> p = Point(10, 10)
>>> p.__x
...
AttributeError: &#39;Point&#39; object has no attribute &#39;__x&#39;
>>> p.__f()
...
AttributeError: &#39;Point&#39; object has no attribute &#39;__f&#39;

_repr_

Try to print Point Example:

>>> p = Point(10, 10)
>>> p
<point.Point object at 0x000000000272AA20>

Usually, this is not the output we want, we want What you need is:

>>> p
Point(10, 10)

Add the special method __repr__ to achieve:

class Point:
  def __repr__(self):
    return &#39;Point({}, {})&#39;.format(self.__x, self.__y)

It is not difficult to see that the interactive mode actually calls repr(p) when printing p:

>>> repr(p)
'Point(10, 10)'

_str_

If __str__ is not provided, str() defaults to the result of repr().
Both are representations of objects in string form, but there are still some differences. Simply put, the results of repr() are for the interpreter and are usually legal Python code, such as Point(10, 10); while the results of str() are for the user and are more concise, such as (10, 10).

According to this principle, we provide the definition of __str__ for Point as follows:

class Point:
  def __str__(self):
    return &#39;({}, {})&#39;.format(self.__x, self.__y)

_add_

The addition of two coordinate points is a very reasonable requirement.

>>> p1 = Point(10, 10)
>>> p2 = Point(10, 10)
>>> p3 = p1 + p2
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: &#39;Point&#39; and &#39;Point&#39;

Add the special method __add__ to do:

class Point:
  def __add__(self, other):
    return Point(self.__x + other.__x, self.__y + other.__y)

>>> p3 = p1 + p2
>>> p3
Point(20, 20)

This is just like operator overloading in C.
Python's built-in types, such as strings and lists, all have "overloaded" operators.

There are many special methods, so I won’t introduce them one by one here.

Inheritance

Give one of the most common examples in textbooks. Circle and Rectangle inherit from Shape. Different shapes have different calculation methods for area.

# shape.py

class Shape:
  def area(self):
    return 0.0
    
class Circle(Shape):
  def __init__(self, r=0.0):
    self.r = r

  def area(self):
    return math.pi * self.r * self.r

class Rectangle(Shape):
  def __init__(self, a, b):
    self.a, self.b = a, b

  def area(self):
    return self.a * self.b

Usage is relatively straightforward:

>>> from shape import *
>>> circle = Circle(3.0)
>>> circle.area()
28.274333882308138
>>> rectangle = Rectangle(2.0, 3.0)
>>> rectangle.area()
6.0

If Circle does not define its own area:

class Circle(Shape):
  pass

Then it will inherit the area of ​​the parent class Shape:

>>> Shape.area is Circle.area
True

Once the Circle After defining your own area, the area inherited from Shape will be overwritten:

>>> from shape import *
>>> Shape.area is Circle.area
False

It can be more obvious through the class dictionary See this clearly:

>>> Shape.__dict__[&#39;area&#39;]
<function Shape.area at 0x0000000001FDB9D8>
>>> Circle.__dict__[&#39;area&#39;]
<function Circle.area at 0x0000000001FDBB70>

So, when a subclass overrides the method of the parent class, it actually just binds the same property name to a different function object. It can be seen that Python does not have the concept of override.

Similarly, it is okay even if Shape does not define area. Shape, as an "interface", cannot be guaranteed by grammar.

You can even add methods dynamically:

class Circle(Shape):
  ...
  # def area(self):
    # return math.pi * self.r * self.r

# 为 Circle 添加 area 方法。
Circle.area = lambda self: math.pi * self.r * self.r

Dynamic languages ​​are generally so flexible, and Python is no exception.

The first sentence of the official Python tutorial "9. Classes" is:

Compared with other programming languages, Python's class mechanism adds classes with a minimum of new syntax and semantics.

Python implements the class mechanism with minimal new syntax and semantics, which is indeed amazing, but it also makes C/Java programmers feel quite uncomfortable.

Polymorphism

As mentioned before, Python does not have the concept of override. Strictly speaking, Python does not support "polymorphism".

为了解决继承结构中接口和实现的问题,或者说为了更好的用 Python 面向接口编程(设计模式所提倡的),我们需要人为的设一些规范。

请考虑 Shape.area() 除了简单的返回 0.0,有没有更好的实现?

以内建模块 asyncio 为例,AbstractEventLoop 原则上是一个接口,类似于 Java 中的接口或 C++ 中的纯虚类,但是 Python 并没有语法去保证这一点,为了尽量体现 AbstractEventLoop 是一个接口,首先在名字上标志它是抽象的(Abstract),然后让每个方法都抛出异常 NotImplementedError。

class AbstractEventLoop:
  def run_forever(self):
    raise NotImplementedError
  ...

纵然如此,你是无法禁止用户实例化 AbstractEventLoop 的:

loop = asyncio.AbstractEventLoop()
try:
  loop.run_forever()
except NotImplementedError:
  pass

C++ 可以通过纯虚函数或设构造函数为 protected 来避免接口被实例化,Java 就更不用说了,接口就是接口,有完整的语法支持。

你也无法强制子类必须实现“接口”中定义的每一个方法,C++ 的纯虚函数可以强制这一点(Java 更不必说)。

就算子类「自以为」实现了“接口”中的方法,也不能保证方法的名字没有写错,C++ 的 override 关键字可以保证这一点(Java 更不必说)。

静态类型的缺失,让 Python 很难实现 C++ / Java 那样严格的多态检查机制。所以面向接口的编程,对 Python 来说,更多的要依靠程序员的素养。

回到 Shape 的例子,仿照 asyncio,我们把“接口”改成这样:

class AbstractShape:
  def area(self):
    raise NotImplementedError

这样,它才更像一个接口。

super

有时候,需要在子类中调用父类的方法。

比如图形都有颜色这个属性,所以不妨加一个参数 color 到 __init__:

class AbstractShape:
  def __init__(self, color):
    self.color = color

那么子类的 __init__() 势必也要跟着改动:

class Circle(AbstractShape):
  def __init__(self, color, r=0.0):
    super().__init__(color)
    self.r = r

通过 super 把 color 传给父类的 __init__()。其实不用 super 也行:

class Circle(AbstractShape):
  def __init__(self, color, r=0.0):
    AbstractShape.__init__(self, color)
    self.r = r

但是 super 是推荐的做法,因为它避免了硬编码,也能处理多继承的情况。

相关推荐:

Python的环境配置解析


The above is the detailed content of Detailed explanation of classes, inheritance and polymorphism in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn