= 모듈 = == 모듈ì´ëž€? == * 파ì´ì¬ 프로그램 íŒŒì¼ í˜¹ì€ C 확장 íŒŒì¼ * 프로그램(함수, í´ëž˜ìŠ¤... )ê³¼ ë°ì´í„°ë¥¼ ì •ì˜ * 사용ìžê°€ ëª¨ë“ˆì— ì •ì˜ëœ 함수나 ë³€ìˆ˜ì˜ ì´ë¦„ì„ ì‚¬ìš©í•˜ë„ë¡ í—ˆìš©í•˜ëŠ”ê²ƒ ( = ë¼ì´ë¸ŒëŸ¬ë¦¬) == ëª¨ë“ˆì„ ì™œ ì‚¬ìš©í• ê¹Œ? == * 코드를 재사용 í• ìˆ˜ 있다. * ëª¨ë“ ê²ƒì´ ëª¨ë“ˆë‹¨ìœ„ë¡œ 분리ë˜ì–´ 있다. * 별ë„ì˜ ì´ë¦„ê³µê°„ì´ ìžˆì–´ì„œ 다른 모듈과 겹치지 않기 ë•Œë¬¸ì— ë…립ì ì¸ ìž‘ì—…ì´ ê°€ëŠ¥í•˜ë‹¤. == 모듈만들기(간단하게..) == {{{~cpp c = 2 def add(a, b): return a+b def mul(a, b): return a*b }}} mymath.py ë¼ëŠ” 파ì¼ë¡œ ì €ìž¥í•œë‹¤.. {{{~cpp >>> import mymath >>> dir(mymath) ['__builtins__', '__doc__', '__file__', '__name__', 'add', 'c', 'mul'] >>> mymath <module 'mymath' from 'C:\Python22\mymath.py'> >>> >>> mymath.c 2 >>> mymath.add <function add at 0x00A927E0> >>> mymath.add(3,4) 7 >>> mymath.mul(4,6) 24 }}} = ì´ë¦„공간 = * ì´ë¦„공간 = ì´ë¦„ì´ ì €ìž¥ë˜ëŠ” 공간 * ìžê²©ë³€ìˆ˜ = ddd.sss ê°™ì´ ì´ë¦„ê³µê°„ì´ ëª…í™•ížˆ 나타나 있는 변수. * 무ìžê²©ë³€ìˆ˜ = ktf ê°™ì´ ê³µê°„ì´ ëª…í™•í•˜ì§€ ì•Šì€ ë³€ìˆ˜. == ì „ì—공간, 지ì—공간 == globals(), locals() {{{~cpp a = 1 b = 2 def f(): localx = 10 localy = 20 print 'ì „ì—변수:', globals() print '지ì—변수:', locals() f() print '모듈 수준ì—ì„œì˜ ì „ì—변수:', globals() print '모듈 수준ì—ì„œì˜ ì§€ì—변수:', locals() }}} ì¶œë ¥ê°’ {{{~cpp ì „ì—변수 a,b 지ì—변수 localx,localy 아래 ë‘개는.. a,b,f() }}} == 모듈 ê³µê°„ì˜ ì´ë¦„ 알아보기 == ì–´ë– í•œ ì†ì„±ì„ ì •ì˜í•˜ê³ 있는가... {{{~cpp >>> import string >>> dir(string) ['_StringTypes', '__builtins__', '__doc__', '__file__', '__name__', '_float', '_idmap', '_idmapL', '_int', '_long', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rstrip', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill'] }}} == ëª¨ë“ˆì˜ ì´ë¦„ 공간얻기 == {{{~cpp 너무길다..ì§ì ‘보여주ìž..!! import string string.__dict__ }}} == ì´ë¦„ê³µê°„ì„ ê°–ëŠ”ê²ƒë“¤ == * 모듈, 함수, í´ëž˜ìŠ¤ 등.. * 외부ì—ì„œ ê°’ ì„¤ì •ì´ ê°€ëŠ¥í•¨.. {{{~cpp >>> string.b Traceback (most recent call last): File "<pyshell#17>", line 1, in ? string.b AttributeError: 'module' object has no attribute 'b' >>> string.b=2 >>> string.b 2 }}} == 모듈 import 하기 == * import 모듈명 (부르기..) * from 모듈명 import ì´ë¦„ (ì´ë¦„.. 사용) * from 모듈명 import* (모ë‘.. 사용) * import 모듈명 as 새ì´ë¦„ (바꾸기..) * from 모듈명 import ì´ë¦„ as 새ì´ë¦„ (바꾸기.. ) ---- [2학기파ì´ì„ 스터디]