python __all__含义

简介: 当我们创建一个文件时 test1.py

当我们创建一个文件时 test1.py

#!/usr/bin/env python3
__arg1 = "lihuanyu01"
_arg2 = "lihuanyu02"
arg03 = "lihuanyu03"
def show():
    print("lihuanyu")

但我们使用指令from test1 import *,将所用的模块都导入进来时候,我们dir( )查看内置函数

['In',
 'Out',
 '_',
 '__',
 '___',
 '__builtin__',
 '__builtins__',
 '__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_dh',
 '_i',
 '_i1',
 '_i2',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 'arg03',
 'exit',
 'get_ipython',
 'quit',
 'show']

我们发现不包含__arg1_arg2,这说明只导入了不以下划线开始的所有属性。

当们创建一个package(test)

init.py的程序如下

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 16 21:58:20 2020
@author: lihuanyu
"""
__arg1 = "lihuanyu01"
_arg2 = "lihuanyu02"
arg03 = "lihuanyu03"
def show():
    print("lihuanyu")

但我们使用指令from test import *,将所用的模块都导入进来时候,我们用dir( )查看内置函数。

['In',
 'Out',
 '_',
 '_2',
 '__',
 '___',
 '__builtin__',
 '__builtins__',
 '__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_dh',
 '_i',
 '_i1',
 '_i2',
 '_i3',
 '_i4',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 'arg03',
 'exit',
 'get_ipython',
 'quit',
 'show']

不包含__arg1_arg2,这说明以包的形式导入,也不会将含有下划线的属性导入。

__all__操作

我们在__init__.py引入__all__ =[""],代码如下

"""
Created on Mon Nov 16 21:58:20 2020
@author: lihuanyu
"""
__arg1 = "lihuanyu01"
_arg2 = "lihuanyu02"
arg03 = "lihuanyu03"
__all__ =[""]
def show():
    print("lihuanyu")

则通过dir()得到的属性如下所示

['In',
 'Out',
 '_',
 '__',
 '___',
 '__builtin__',
 '__builtins__',
 '__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_dh',
 '_i',
 '_i1',
 '_i2',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 'exit',
 'get_ipython',
 'quit']

此时我们定义的__arg1_arg2arg3均为含有,这是因为__all__是空列表,屏蔽了所有属性的导入。

我们在__init__.py引入__all__ = ["__arg1"],代码如下

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 16 21:58:20 2020
@author: lihuanyu
"""
__all__ = ["__arg1"]
__arg1 = "lihuanyu01"
_arg2 = "lihuanyu02"
arg03 = "lihuanyu03"
def show():
    print("lihuanyu")

结果为

['In',
 'Out',
 '_',
 '__',
 '___',
 '__arg1',
 '__builtin__',
 '__builtins__',
 '__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_dh',
 '_i',
 '_i1',
 '_i2',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 'exit',
 'get_ipython',
 'quit']

此时只有__arg1属性

总结

当模块设有 all 变量时,只能导入该变量指定的成员,未指定的成员是无法导入的。

相关文章
|
1月前
|
数据挖掘 开发者 索引
【Python】已解决:ValueError: If using all scalar values, you must pass an index
【Python】已解决:ValueError: If using all scalar values, you must pass an index
173 0
|
2月前
|
自然语言处理 Python
python技巧:数组排序sort,all方法
python技巧:数组排序sort,all方法
|
9月前
|
Python
Python 关于模块的几点介绍 。和。。和__all__和__main___和__file__
用来定义我们导出的内容可以有哪些的一个编码方式
27 0
|
9月前
|
Python
65 python - 模块中的__all__
65 python - 模块中的__all__
33 0
|
3月前
|
Python
Python 教程之运算符(7)—— Any All
Python 教程之运算符(7)—— Any All
37 0
Python 教程之运算符(7)—— Any All
|
8月前
|
Python
Python 教程之运算符(7)—— Any All
Python 教程之运算符(7)—— Any All
40 0
|
Python
【Python】__all__的作用/模块导入
【Python】__all__的作用/模块导入
63 0
|
存储 Java Unix
Python中的模块、包、import module1,mudule2、from…import、from … import *、as、定位模块、模块中的__all__和__name__、模块的注意点
在Python中用关键字import来引入某个模块,比如要引用模块math,就可以在文件最开始的地方用import math来引入想一想:为什么必须加上模块名调用呢?因为可能存在这样一种情况:在多个模块中含有相同名称的函数,此时如果只是通过函数名来调用,解释器无法知道到底要调用哪个函数。所以如果像上述这样引入模块的时候,调用函数必须加上模块名通过这种方式引入的时候,调用函数时只能给出函数名,不能给出模块名,但是当两个模块中含有相同名称函数的时候,后面一次引入会覆盖前一次引入。也就是说假如模块A中有函数fun
189 1
Python中的模块、包、import module1,mudule2、from…import、from … import *、as、定位模块、模块中的__all__和__name__、模块的注意点
|
Python
python中的*args与**kwargs的含义与作用
python中的*args与**kwargs的含义与作用
155 0
|
数据安全/隐私保护 Python
Python 常见问题 - pip install 指定 poetry 导出的 requirements.txt,报错 ERROR: In --require-hashes mode, all requirements must have their versions pinned with ==. These do not: cffi>=1.1 from https://.....
Python 常见问题 - pip install 指定 poetry 导出的 requirements.txt,报错 ERROR: In --require-hashes mode, all requirements must have their versions pinned with ==. These do not: cffi>=1.1 from https://.....
763 0
Python 常见问题 - pip install 指定 poetry 导出的 requirements.txt,报错 ERROR: In --require-hashes mode, all requirements must have their versions pinned with ==. These do not: cffi>=1.1 from https://.....