day 4 __all__ 包 __init__.py

Posted 不要被骄傲遮蔽了双眼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day 4 __all__ 包 __init__.py相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

__all__ 是导入的简写吗? [复制]

__all__ 在 __init__.py 中不生效? [复制]

day_4:内置函数

Python中__init__.py文件作用之我见

day_5.11 py main

关于python 的__init__.py 以及 __all__ 的用法