十三. __all__

十三. __all__

前面模块介绍章节其实已经介绍过了 __all__ 的使用, 它用来在使用 from [模块名] import * 的时候, 针对这个 * 来限制能够使用的属性

🐞文件 "test.py" 内容
__all__ = ["Foo","Bar"]  # 如此只能使用 Foo,Bar 两个属性

class Foo:
    def __init__(self):
        print("Foo")

class Bar:
    def __init__(self):
        print("Bar")

class Abc:
    def __init__(self):
        print("Abc")
🐞当前执行文件内容
from test import * 

f = Foo()  # Foo
b = Bar()  # Bar
A = Abc()  # 报错 : "NameError"  这个名字没有找到

posted @ 2021-01-05 22:21  给你骨质唱疏松  阅读(118)  评论(0)    收藏  举报