浅谈Python中的包

Package的定义(你以为的)

  • 你在很多的地方都能看到关于package的定义:在Python中在当前目录下有__init__.py文件的目录即为一个package。

  • 嗯,包括python目前的官网文档也是类似这么介绍的

    https://docs.python.org/zh-cn/3.9/tutorial/modules.html#packages
    
    Python 只把含 __init__.py 文件的目录当成包
    https://docs.python.org/3.9/tutorial/modules.html#packages
    
    The __init__.py files are required to make Python treat directories containing the file as packages

3.9如此,3.11也是的,可能是文档维护人员的工资没发?

Package的定义(应该是这样的)

  • 然在上面的文档中,有这样一句

    • 包是一种用“点式模块名”构造 Python 模块命名空间的方法
    • Packages are a way of structuring Python’s module namespace by using “dotted module names”.

我认为是比较符合当前的设定的

是否需要初始化文件

https://realpython.com/lessons/package-initialization/

  • 看上面这个文章,尾部有这样的一段说明
Much of the Python documentation states that an __init__.py file must be present in the package directory when creating a package. This was once true. It used to be that the very presence of __init__.py signified to Python that a package was being defined. The file could contain initialization code or even be empty, but it had to be present.

Starting with Python 3.3, Implicit Namespace Packages were introduced. These allow for the creation of a package without any __init__.py file. Of course, it can still be present if package initialization is needed. But it is no longer required.
  • 简陋的翻译一下
很多的python文档说__init__.py必须要存在于package目录中,当你创建一个package的时候(注:事实上IDE如pycharm也是这么做的,默认就给你创建了..)。这曾经是对的。曾经它出现在一个python的目录中就表示了你在创建一个包。这个__init__.py文件可以包含初始化文件或者为空,但它必须存在。

从Python3.3开始,开始引入了隐式命名空间包的概念。这就允许了创建包的时候不需要__init__.py。当然如果一个包要初始化,它仍然是需要的。但是,不再是必要的!

PEP 420 Implicit Namespace Packages

  • 在Specification章节

    Regular packages will continue to have an __init__.py and will reside in a single directory.
    
    Namespace packages cannot contain an __init__.py
  • 它也提到了namespace packages和regular packages的区别

    • Portions of namespace packages need not all come from the same directory structure, or even from the same loader. Regular packages are self-contained: all parts live in the same directory hierarchy.
    • Namespace packages have no __file__ attribute.
    • Namespace packages’ __path__ attribute is a read-only iterable of strings, which is automatically updated when the parent path is modified.
    • Namespace packages have no __init__.py module.
    • Namespace packages have a different type of object for their __loader__ attribute

这个区别实在不好测试,比想象的要繁琐很多

一个简单的测试

  • 目录结构

    D:\pythonProject\AutoTest\PackageTest (7.08KB)
    
    ├── NamespacePack (2b)
    
    │   ├── demoNP.py (24b)
    
    ├── RegularPack (35b)
    
    │   ├── demoRP.py (24b)
    
    │   ├── __init__.py (2b)
    
    └── testPack.py (114b)
    
    
  • 区别就是在pycharm上创建一个目录NamespacePack,创建一个package(RegularPack)

  • demoNP.py

    nameNP = 'NamespacePack'
  • demoRP.py

    nameRP = 'RegularPack'
  • __init__.py为空

  • testPack.py

    from NamespacePack.demoNP import nameNP
    from RegularPack.demoRP import nameRP print(nameNP) # 输出NamespacePack
    print(nameRP) # 输出RegularPack
  • 跟普通的包似乎没有任何区别(至少我感知不到)

说在最后

  • 初学者(我也算)无需纠结细节
  • 只需知道python中的包并不一定要__init__.py,over

浅谈Python中的包的更多相关文章

  1. 浅谈python中得import xxx,from xxx import xxx, from xxx import *

    在python中import跟from import都是用来导入的,但是导入的机制不同 1.import xxx:导入模块,或者文件夹,对于调用模块或者文件夹中子模块的变量或者函数,需要使用" ...

  2. 浅谈python中的“ ==” 与“ is”

    在python中,== 与 is 之间既有区别,又有联系,本文将通过实际代码的演示,力争能够帮助读到这篇文章的朋友以最短的时间理清二者的关系,并深刻理解它们在内存中的实现机制.扯淡的话不多说,下面马上 ...

  3. 浅谈python中文件和文件夹的相关操作

    文件操作 文件的打开与关闭 打开文件 使用open(文件名,访问方式)函数,可以打开一个已存在的文件,或者创建一个新的文件. 示例如下: f = open('test.txt') # 访问方式可以省略 ...

  4. 浅谈python中字典append 到list 后值的改变问题

    看一个例子 ? 1 2 3 4 d={'test':1} d_test=d d_test['test']=2 print d 如果你在命令行实践的话,会发现你改动的是d_test ,但是d 也跟着改变 ...

  5. 浅谈python中的闭包函数

    闭包函数初探 通常我们定义函数都是这样定义的 def foo(): pass 其实在函数式编程中,函数里面还可以嵌套函数,如下面这样 def foo(): print("hello worl ...

  6. 浅谈python中的“ ==” 与“ is”、还有cmp

    总之,比较内容相等使用 ‘==’ 1.is" 是用来比较 a 和 b 是不是指向同一个内存单元,而"=="是用来比较 a 和 b指向的内存单元中的值是不是相等 2.pyt ...

  7. 浅谈Python中函数式编程、面向对象编程以及古怪的PythonIC

    1.函数式编程作为结构化编程的一种,正在受到越来越多的重视.那么什么事函数式编程呢? 在维基百科中给出了详细的定义,函数式编程又称泛函数编程,是一种编程规范,它将函数运算视为数学上的函数计算.简单的来 ...

  8. 浅谈python中selenium库调动webdriver驱动浏览器的实现原理

    最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? from se ...

  9. 浅谈python中__str__和__repr__的区别

    很多时候我们在创建一个类的时候,在终端打印类或者查看的时候一般都不会得到一个太满意的结果 class T: def __init__(self): self.color="red" ...

  10. 浅谈Python 中 __getattr__与__getattribute__的区别

    __getattr__与__getattribute__均是一般实例属性截取函数(generic instance attribute interception method),其中,__getatt ...

随机推荐

  1. PHP使用PHPmailer类和smtp发送邮件

    开启邮件smtp服务 设置授权码 引入phpmailer类,smtp类本地下载https://github.com/PHPMailer/PHPMailer //下载PHPMailer并开启php_op ...

  2. mindxdl---common---db_handler.go

    // Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.// Package common this file ...

  3. 同步与异步、阻塞与非阻塞、创建进程的多种方式、进程间数据隔离、进程的join方法、IPC机制等

    目录 同步与异步 阻塞与非阻塞 综合使用 创建进程的多种方式 进程间数据隔离 进程的join方法 IPC机制 生产者消费者模型 进程对象的多种方法 守护进程 僵尸进程与孤儿进程 多进程数据错乱问题 同 ...

  4. MySQL57 zip安装

    引用:MySQL5.7的.zip文件的配置安装   由于MySQL5.7之后在javaEE中交互的端口发生了变化,而MySQL官网中5.6.5.7版本64位的只有.zip文件,而.zip文件不像直接下 ...

  5. c++详细学习——引用

    1 引用(reference) 引用是一个变量的别名,故引用在申明的时候必须给初始值,从此他们就建立了"不能离婚的婚姻关系",改变引用就会改变被引用的原变量 1 int main( ...

  6. audio解决不能自动播放问题

    问题描述 无法实现打开网页就能自动播放音乐 正常情况下使用autoplay即可实现自动播放,但是现在打开网页该参数无效 原因分析: 根据最新的规范,Chrome系浏览器,没有交互过的网站默认禁止自动播 ...

  7. dojo 访问 VS 创建的Json文件 汉字乱码

    通过VS创建了一个json文件,直接保存成了文件放到了Web根目录下. 通过dojo的dojo/request访问,返回的汉字都是乱码. 通过以下方案解决. 用记事本把josn文件打开,然后点击另存为 ...

  8. Map的遍历方式,常用的key-value遍历方式

    在开发过程中经常会遇到 map 的遍历,本文将会介绍四种常用的 key-value 遍历方式 说明: 增强 for 循环遍历 先取出 map 的 keySet,进行遍历,再取出对应 key 的 val ...

  9. 回溯法求解n皇后问题(复习)

    回溯法 回溯法是最常用的解题方法,有"通用的解题法"之称.当要解决的问题有若干可行解时,则可以在包含问题所有解的空间树中,按深度优先的策略,从根节点出发搜索解空间树.算法搜索至解空 ...

  10. python与数值计算环境安装

    数值计算的编程的软件很多种,也见过一些编程绘图软件的对比. 利用Python进行数值计算,需要用到numpy(矩阵) ,scipy(公式符号), matplotlib(绘图)这些工具包. 1.Linu ...