Detailed examples of classes, inheritance and polymorphism in Python

黄舟
Release: 2017-07-17 15:02:32
Original
1424 people have browsed it

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
Copy after login

The most basic one is the init method, which is equivalent to the constructor of C++/Java. Methods with double underscores are special methods. In addition to init, there are many more, which will be introduced later.

The parameter self is equivalent to this in C++, 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)
Copy after login

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;>
Copy after login

Point is an instance of type, which is the same thing as p is an instance of Point.

Now add the method set:

class Point:
  ...
  def set(self, x, y):
    self.x, self.y = x, y
Copy after login
>>> p = Point(10, 10)
>>> p.set(0, 0)
>>> p.x, p.y
(0, 0)
Copy after login

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

>>> Point.set(p, 0, 0)
>>> p.x, p.y
(0, 0)
Copy after login

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
Copy after login

The difference with C++ is that, "Member variables" must be prefixed with self., otherwise they will become attributes of the class (equivalent to C++ static members) rather than attributes of the object.

Access control

Python does not have such access control as public / protected / private. If you insist on expressing "private", it is customary to add 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
Copy after login

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;
Copy after login

_repr_

Try to print Point instance:

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

Usually, this is not the output we want, what we want is:

>>> p
Point(10, 10)
Copy after login

Add the special method repr to achieve:

class Point:
  def repr(self):
    return &#39;Point({}, {})&#39;.format(self.x, self.y)
Copy after login

It is not difficult to see that the interactive mode actually prints p Repr(p) is called:

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

_str_

If str is not provided, str() defaults to the result of repr().
Both are representations of objects in the form of string, 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)
Copy after login

_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;
Copy after login

Add the special method add to do:

class Point:
  def add(self, other):
    return Point(self.x + other.x, self.y + other.y)
Copy after login
>>> p3 = p1 + p2
>>> p3
Point(20, 20)
Copy after login

This is just like the operatoroverloading in C++.
Python's built-in types, such as strings and lists, are "overloaded" with the + operator.

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
Copy after login

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
Copy after login

If Circle does not define its own area:

class Circle(Shape):
  pass
Copy after login

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

>>> Shape.area is Circle.area
True
Copy after login

Once Circle defines its own area, the area inherited from Shape is overwritten:

>>> from shape import *
>>> Shape.area is Circle.area
False
Copy after login

This can be seen more clearly through the class dictionary:

>>> Shape.dict[&#39;area&#39;]
<function Shape.area at 0x0000000001FDB9D8>
>>> Circle.dict[&#39;area&#39;]
<function Circle.area at 0x0000000001FDBB70>
Copy after login

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
Copy after login

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 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".

In order to solve the problem of interface and implementation in the inheritance structure, or in order to better use Python for interface-oriented programming (advocated by Design Pattern), we need to set some artificial standards.

Please consider whether there is a better implementation of Shape.area() besides simply returning 0.0?

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

class AbstractEventLoop:
  def run_forever(self):
    raise NotImplementedError
  ...
Copy after login

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

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

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

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

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

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

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

class AbstractShape:
  def area(self):
    raise NotImplementedError
Copy after login

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

super

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

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

class AbstractShape:
  def init(self, color):
    self.color = color
Copy after login

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

class Circle(AbstractShape):
  def init(self, color, r=0.0):
    super().init(color)
    self.r = r
Copy after login

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

class Circle(AbstractShape):
  def init(self, color, r=0.0):
    AbstractShape.init(self, color)
    self.r = r
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template