1.__all__的作用

  • 如果一个文件中有__all__变量,那么也就意味着这个变量中的元素,不会被from xxx import *时导入
__all__ = ["test1","num"]    #只能让调用test1,num1
def test1():
print("----test1") def test2():
print("----test2") num = 1 class Dog(object):
pass
In [6]: from sendmsg import *   #只能调用__all__里面规定的方法

In [7]: test1()
----test1 In [8]: test2()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-35ebc1c532fc> in <module>()
----> 1 test2() NameError: name 'test2' is not defined In [9]: num
Out[9]: 1 In [10]: Dog()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-10-ff6dc2469c3e> in <module>()
----> 1 Dog() NameError: name 'Dog' is not defined
In [1]: import sendmsg    #通过这个方式不受影响

In [2]: sendmsg.
sendmsg.Dog sendmsg.py sendmsg.test2
sendmsg.num sendmsg.test1 In [2]: sendmsg.
sendmsg.Dog sendmsg.py sendmsg.test2
sendmsg.num sendmsg.test1 In [2]: sendmsg.test1()
----test1 In [3]: sendmsg.test2()
----test2

2.包

  • 包:在一个文件夹下有多个模块,并且有个__init__.py文件
  • 模块:1个py文件
###   sendmsg.py
def test1():
print("--sendmsg-test1---") #### recvmsg.py
def test2():
print("---recvmsg--test2---")

  1)版本1:

.
├── infordisplay.py
└── Msg #具有很多模块的集合场所
├── recvmsg.py
└── sendmsg.py
####   IPython 2.4.1     python2认为这只是文件夹

In [1]: import Msg
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-32b2df17b362> in <module>()
----> 1 import Msg ImportError: No module named Msg
###   iPython 3.5.2   python认为此文件夹是包

In [1]: import Msg

In [2]: Msg.sendmsg.test1()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-889fe4d38288> in <module>()
----> 1 Msg.sendmsg.test1() AttributeError: module 'Msg' has no attribute 'sendmsg'

  2)版本2:__init__.py  让文件夹变成包

.
├── infordisplay.py
└── Msg #包
├── __init__.py
├── recvmsg.py
└── sendmsg.py

    

###   IPython 2.4.1

In [1]: import Msg

In [2]: Msg.sendmsg.test1()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-889fe4d38288> in <module>()
----> 1 Msg.sendmsg.test1() AttributeError: 'module' object has no attribute 'sendmsg'
####   Python 3.5.2

In [1]: import Msg

In [2]: Msg.sendmsg.test1()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-889fe4d38288> in <module>()
----> 1 Msg.sendmsg.test1() AttributeError: module 'Msg' has no attribute 'sendmsg'

  3)版本3:

###     Msg/__init__.py
__all__ = ["sendmsg"] print("when import this bag ,do this __init__.py ") import sendmsg
####    IPython 2.4.1   可以直接执行
### python3 不能执行 In [1]: import Msg
when import this bag ,do this __init__.py In [2]: Msg.sendmsg.test1()
--sendmsg-test1---

  4)版本4:最终版本

##      Msg/__init__.py

__all__ = ["sendmsg"]

print("when import this bag ,do this __init__.py ")

from . import sendmsg

    #. 当前目录
####   python 2 3 都可以执行
In [1]: import Msg
when import this bag ,do this __init__.py In [2]: Ms
Msg Msg/ In [2]: Msg.sendmsg.test1()
--sendmsg-test1---

day 4 __all__ 包 __init__.py的更多相关文章

  1. Python模块包中__init__.py文件的作用

    转载自:http://hi.baidu.com/tjuer/item/ba37ac4ce7482a0f6dc2f08b 模块包: 包通常总是一个目录,目录下为首的一个文件便是 __init__.py. ...

  2. pydev package包中__init__.py作用

    Eclipse用pydev,新建一个pydev package时,总会自动地生成一个空的__init__.py文件. 原来在python模块的每一个包中,都有一个__init__.py文件(这个文件定 ...

  3. python基础===包的导入和__init__.py的介绍

    转自:https://www.cnblogs.com/botoo/p/8241522.html 调用同级目录: – src |– mod.py |– test.py 若在程序test.py中导入模块m ...

  4. Python模块包(pycharm右键创建文件夹和python package的区别)中__init__.py文件的作用

    在eclipse中用pydev开发Python脚本时,我遇到了一个这样的现象,当我新建一个pydev package时,总会自动地生成一个空的__init__.py文件,因为是python新手,所以很 ...

  5. python 项目中包中__init__.py文件的作用

    开发python项目时,我遇到了一个这样的现象,当我新建一个pythonpackage时,总会自动地生成一个空的__init__.py文件,因为是python新手,所以很不了解这个空文件的作用是什么, ...

  6. python中__init__.py文件的作用

    问题 在执行models.py时,报ImportError:No module named transwarp.db的错误,但明明transwarp下就有db.py文件,路径也没有错误.真是想不通.后 ...

  7. Python的__init__.py用法

    python中包的引入,对于大型项目中都会使用到这个功能,把实现不同功能的python文件放在一起,组成不同lib库,然后在其他地方调用. 包,python源文件+__init__.py 模块,pyt ...

  8. python2中的__init__.py文件的作用

    python2中的__init__.py文件的作用: 1.python的每个模块的包中,都必须有一个__init__.py文件,有了这个文件,我们才能导入这个目录下的module. 2.__init_ ...

  9. Python __init__.py 文件使用

    __init__.py的主要作用是: 1. Python中package的标识,不能删除 2. 定义__all__用来模糊导入 3. 编写Python代码(不建议在__init__中写python模块 ...

随机推荐

  1. bzoj 2111: [ZJOI2010]Perm 排列计数 (dp+卢卡斯定理)

    bzoj 2111: [ZJOI2010]Perm 排列计数 1 ≤ N ≤ 10^6, P≤ 10^9 题意:求1~N的排列有多少种小根堆 1: #include<cstdio> 2: ...

  2. FZU-1759 Super A^B mod C---欧拉降幂&指数循环节

    题目链接: https://cn.vjudge.net/problem/FZU-1759 题目大意: 求A^B%C 解题思路: 注意,这里long long需要用%I64读入,不能用%lld #inc ...

  3. Intellij IDEA Organize Imports

    使用Eclipse进行开发时,我喜欢用Ctrl + Shift + O快捷键管理Java类的导入,它可以导入所需的Java类,去除不需要的Java类. Eclipse的Organize Imports ...

  4. Springmvc 进行数据类型转换

    SpringMVC进行一些常用的数据类型转换,这里以Date 数据类型的转换为例. SpringMVC表单中输入日期,一般都是以字符串的形式输入,如何将字符形式的日期转换为Date 类型的呢?这里只需 ...

  5. 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)

    链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...

  6. 【[HNOI2004]L语言】

    \(Trie\)树+\(DP\) 我们只需要做一个存在性dp就好了 对于每一个字符串,我们设\(f[i]\)表示从\(1\)到\(i\)位是否能被完全匹配 首先\(f[0]=1\),之后我们对于每一个 ...

  7. 网易mumu模拟器配置文件和修改adb port位置

    网易mumu模拟器配置文件在安装目录下 emulator\nemu\vms\myandrovm_vbox86下的myandrovm_vbox86.nemu文件中 修改port位置

  8. iview(DatePicker)时间转入后台少一天 解决方案

    后台注解 前台: 加个事件 @on-change @on-change="getStartTime" getStartTime(starTime) { this.leaveReco ...

  9. FreeRTOS 和uCOS II的简单比较

    转载:http://www.viewtool.com/bbs/forum.php?mod=viewthread&tid=114 这是两种RTOS, 现在粗略比较一下. freeRTOS比uCO ...

  10. 绘图驱动-OSD原理1

    OSD(On Screen Display)是屏幕显示技术的一种,用于在显示终端上显示字符.图形和图像.实现的过程为:存储器(一般为内存的一段)的内容与显示终端上的像素一一对应.这种一一对应的关系一般 ...