파이썬 __all__ 사용

파이썬 모듈의 __all__은 다음과 같이 모듈 가져 오기 를 제한하는 데 사용됩니다. from module import *

이때 가져온 모듈이 __all__ 속성을 정의하면 __all__에 지정된 속성, 메서드, 클래스 만 가져올 수 있으며, 정의하지 않으면 모듈의 모든 공용 속성, 메서드 및 클래스를 가져옵니다.

1. 예제 1

# bb.py 
class A () : 
    def __init __ (self, name, age) : 
        self.name = name 
        self.age = age 
class B () : 
    def __init __ (self, name, id) : 
        self.name = name 
        self .id = id 
def fun () : 
    print "func ()가 실행되었습니다!" 
def fun1 () : 
    print "func1 ()이 실행되었습니다!"


# test_bb.py 
from bb import * 
a = A ( 'zhansan', '18') 
print a.name, a.age 
b = B ( "lisi", 1001) 
print b.name, b.id 
fun () 
fun1 ()

运行 结果 :
zhansan 18 
lisi 1001 
func ()가 실행됩니다! 
func1 ()이 실행됩니다!

注:
  由于bb.py中没有定义__all__属性,所以导入了bb.py中所有的公有属性


2.实例2
# bb.py 
__all __ = ( 'A', 'func') 
class A () : 
    def __init __ (self, name, age) : 
        self.name = name 
        self.age = age 
class B () : 
    def __init __ (self, name, id) : 
        self.name = name 
        self.id = id 
def func () : 
    print "func () is run!" 
def func1 () : 
    print "func1 ()이 실행되었습니다!"
 
 
# test_bb.py 
from bb import * 
a = A ( 'zhansan', '18') 
print a.name, a.age 
func () 
# b = B ( "lisi", 1001) 
# func1 ()
#NameError: name 'B' is not defined
#NameError: name 'func1' is not defined  
실행 결과 : 
zhansan 18 
func ()가 실행되었습니다! 

참고 : 
  bb.py에서 __all __ = ( 'A', 'func')가 사용되기 때문에 다른 모듈이이 모듈을 가져올 때 __all__ 만 가져올 수 있습니다. 변수, 메서드, 클래스


3.实例3
# bb.py 
데프 FUNC () : #public 모듈의 방법 print'func이 
    '! () 실행 
: DEF _func () 
    #protected 모듈의 방법을 실행 print'_func ()!' 
데프 __func () : #을 전용 메소드 
    print '__ func ()가 모듈에서 실행됩니다!'

# test_bb.py 
from bb import * #이 메서드는 공용 속성, 메서드 및 클래스 만 가져올 수 있습니다. [단일 밑줄 (보호됨) 또는 이중 밑줄 (개인용)로 시작하는 속성, 메서드 및 클래스는 가져올 수 없음] 
func () 
# _func () 
#__ func () 


실행 결과 : 
func ()가 실행되었습니다! 

참고 : 
  from bb import * -이 메서드는 공용 속성, 메서드 및 클래스 만 가져올 수 있습니다 [단일 밑줄 (보호됨) 또는 이중 밑줄로 가져올 수 없음 처음에 속성, 메서드 및 클래스 (개인)] 
  _func() #NameError: name '_func' is not defined
  __func() #NameError: name '__func' is not defined


4. 예제 4
# bb.py 
__all __ = ( 'func', '__ func', '_ A') #放入__all__中所有属性均可导入,即使是以下划线开头
class _A () : 
    def __init __ (self, name) : 
        self.name = name 
def func () : 
    print "func () is run! " 
def func1 () : 
    print "func1 ()이 실행되었습니다!" 
def _func () : 
    print "_func ()가 실행되었습니다!" 
def __func () : 
    print "__func ()가 실행되었습니다!"


# test_bb.py 
from bb import * 
func () 
# func1 () #func1不在__all__中,无法导入 NameError: name 'func1' is not defined 
#_func () #_func不在__all__中,无法导入 NameError: name '_func' is not defined
__func () #__func在__all__中,可以导入
a = _A ( 'zhangsan') #_A在__all__中,可以导入
print a.name 


실행 결과 : 
func ()가 실행되었습니다! 
__func ()가 실행되었습니다! 
zhangsan 

노트 :
  放入__all__中所有属性均可导入,即使是以下划线开头
  func1() #func1不在__all__中,无法导入 NameError: name 'func1' is not defined
  _func() #_func不在__all__中,无法导入 NameError: name '_func' is not defined
  __func() #__func在__all__中,可以导入
  a=_A('python') #_A在__all__中,可以导入



5.实例5

# bb.py 
def func () : 
    print 'func () is run!' 
def _func () : 
    print '_func ()가 실행되었습니다!' 
def __func () : 
    print '__func () is run!'


# test_bb.py 
from bb import func, _func, __ func #可以通过这种方式导入public,protected,private
func () 
_func () 
__func () 


실행 결과 : 
func ()가 실행되었습니다! 
_func ()가 실행되었습니다! 
__func ()가 실행되었습니다! 

참고 : 
  _func (), __func ()는 " protected , private"권한에 하지만이 방법을 사용하면 직접 가져 와서 접근 할 수 있습니다. 


6. 예제 6

# bb.py 
def func () : 
    print 'func () is run!' 
def _func () : 
    print '_func ()가 실행되었습니다!' 
def __func () : 
    print '__func () is run!'
# test_bb.py 
import bb #可以通过这种方式导入public,protected,private
bb.func () 
bb._func () 
bb .__ func () 

running result : 
func () is run! 
_func () is run! 
__func () is run! 

Note : 
  you can import the module Import 모듈을 찾은 다음 module.XX 메서드를 사용하여 액세스 "public,protected,private"权限的内容

추천

출처blog.csdn.net/weixin_45131345/article/details/111868252