码迷,mamicode.com
首页 > 其他好文 > 详细

day 4 __all__ 包 __init__.py

时间:2017-11-25 20:45:05      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:影响   port   imp   def   https   ges   http   display   input   

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

标签:影响   port   imp   def   https   ges   http   display   input   

原文地址:http://www.cnblogs.com/venicid/p/7896229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!