模块中的__all__

1. 没有__all__

#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Test(object):
    def test(self):
        print("Test类中的test函数")

def test1():
    print("test1函数")


def test2():
    print("test2函数")
from main import *
a = Test()
a.test()
test1()
test2()

2. 模块中有__all__

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__all__ = ["Test", "test1"]
class Test(object):
    def test(self):
        print("Test类中的test函数")

def test1():
    print("test1函数")


def test2():
    print("test2函数")

总结

  • 如果一个文件中有__all__变量,那么也就意味着这个变量中的元素,不会被from xxx import *时导入

results matching ""

    No results matching ""