zoukankan      html  css  js  c++  java
  • 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---
  • 相关阅读:
    Linux 多进程锁的几种实现方案
    Linux man手册没有pthread_mutex_init的解决办法
    IP地址结构信息与字符串相互转化:inet_pton和inet_ntop, etc.
    Linux 将计算md5值功能做成md5命令
    Unix/Linux inet守护进程
    Unix/Linux syslogd守护进程 & 日志记录syslog
    UNP 学习笔记 #11 名字与地址转换
    git 使用总结
    AUPE 输出致标准错误的出错函数分析与实现 err_sys, err_quit, err_doit etc.
    Linux C常见数I/O函数比较: printf, sprintf, fprintf, write...
  • 原文地址:https://www.cnblogs.com/venicid/p/7896229.html
Copyright © 2011-2022 走看看