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

Detailed explanation of classes, inheritance and polymorphism in Python

May 02, 2018 pm 03:36 PM
python Detailed explanation

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 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)
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 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
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 you can clearly see 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 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
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 Example:

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

Usually, this is not the output we want, we want What you need 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 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)
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 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
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 the Circle After defining your own area, the area inherited from Shape will be overwritten:

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

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>
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 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
  ...
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 是推荐的做法,因为它避免了硬编码,也能处理多继承的情况。

相关推荐:

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

Can vscode run ipynb Can vscode run ipynb Apr 15, 2025 pm 07:30 PM

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

Golang vs. Python: Concurrency and Multithreading Golang vs. Python: Concurrency and Multithreading Apr 17, 2025 am 12:20 AM

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

See all articles