Python.pyi扩展中的“i”代表什么?

2024-05-09 22:04:31 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中,.pyi扩展中的“i”代表什么?

PEP-484中,它提到.pyi是“一个存根文件”,但在扩展名上没有助记符帮助。那么“我”是指“包括”吗实现“?”接口“?


Tags: 文件代表pep存根助记符pyi
2条回答

显然,PyCharm创建.pyi文件的目的是:

The *.pyi files are used by PyCharm and other development tools to provide more information, such as PEP 484 type hints, than it is able to glean from introspection of extension types and methods. They are not intended to be imported, executed or used for any other purpose other than providing info to the tools. If you don't use use a tool that makes use of .pyi files then you can safely ignore this file.

See: https://www.python.org/dev/peps/pep-0484/https://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html

此注释位于:python27/Lib/site-packages/wx/core.pyi

我认为.pyi中的i代表“接口”

Java中接口的定义:

An interface in the Java programming language is an abstract type that is used to specify a behaviour that classes must implement

Each Python module is represented by a .pyi "stub". This is a normal Python file (i.e., it can be interpreted by Python 3), except all the methods are empty.

  • 'Mypy'存储库中,它们明确提到“存根”文件作为公共接口:

A stubs file only contains a description of the public interface of the module without any implementations.

因为“接口”在Python中不存在(参见本文SO question between Abstract class and Interface),所以我认为设计人员打算专门为它提供一个特殊的扩展。

pyi实现“存根”文件(来自Martin Fowler的定义)

Stubs: provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.

但是人们对接口比“存根”文件更熟悉,因此选择.pyi而不是.pys更容易避免不必要的混淆。

相关问题 更多 >