[Python응용] 8. 환경변수 등록과 from절과 import절 사용하기

모듈 로딩을 위한 환경 변수 등록은?

import sys
sys.path
['', 'D:\\Work\\Dev\\Python\\python38.zip', 'D:\\Work\\Dev\\Python\\DLLs', 'D:\\Work\\Dev\\Python\\lib', 'D:\\Work\\Dev\\Python', 'D:\\Work\\Dev\\Python\\lib\\site-packages']

from절과 import절의 정의와 활용

정의

활용

from simpleset import union
union([1, 2, 3], [3], [3, 4])
[1, 2, 3, 4]
intersect([1, 2], [1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'intersect' is not defined

활용2

from simpleset import *
dir()
['WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'cmp_to_key', 'difference', 'intersect', 'lru_cache', 'partial', 'partialmethod', 'reduce', 'singledispatch', 'singledispatchmethod', 'total_ordering', 'union', 'update_wrapper', 'wraps']

활용3

import testmodule as test1
hello world
import testmodule as test2 # hello world 출력 안됨
dir()
['WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'cmp_to_key', 'difference', 'intersect', 'lru_cache', 'partial', 'partialmethod', 'reduce', 'singledispatch', 'singledispatchmethod', 'total_ordering', 'union', 'update_wrapper', 'wraps']
test1.printDefaultValue()
2
test1.defaultvalue = 100
test2.printDefaultValue() # 이름은 다르지만 동일한 것을 참조
100