모듈이란?
내장모듈
Python
1 2 3 4 | import math print (math.ceil( 2.9 )) print (math.floor( 2.9 )) print (math.sqrt( 16 )) |
1 2 3 | 3 2 4.0 |
Ruby
1 | puts(Math.sqrt( 16 )) |
1 | 4.0 |
모듈에 없을 때
Python
1 2 3 4 5 6 7 | def egoing_a(): return 'a' #엄청 많은 코드 def k8805_a(): return 'B' #엄청 많은 코드 print (egoing_a()) |
Ruby
1 2 3 4 5 6 7 8 9 | def egoing_a() return 'a' end #엄청 많은 코드 def k8805_a() return 'B' end #엄청 많은 코드 puts(egoing_a()) |
실행결과
1 | a |
모듈의 도입 - 파이썬
Python
egoing.py
1 2 3 4 5 6 | def a(): return 'a' def b(): return 'b' def c(): return 'c' |
k8805.py
1 2 | def a(): return 'B' |
3.py
1 2 3 4 | from egoing import a as z import k8805 as k print (z()) print (k.a()) |
1 2 | a B |
모듈의 도입 - 루비
Ruby
egoing.rb
1 2 3 4 5 6 | module Egoing module_function() def a() return 'a' end end |
k8805.rb
1 2 3 4 5 6 | module K8805 module_function() def a() return 'B' end end |
3.rb
1 2 3 4 | require_relative 'Egoing' require_relative 'K8805' puts(Egoing.a()) puts( K8805 .a()) |
1 2 | a B |
모듈을 통한 중복의 제거 & 재활용성의 증대
Python
egoing.py
1 2 3 4 5 6 | def a(): return 'a' def b(): return 'b' def c(): return 'c' |
4_1.py
1 2 | import egoing print (egoing.a()) |
1 | a |
4_2.py
1 2 | import egoing print (egoing.a()) |
1 | a |
로그인 에플리케이션
Python
auth.py
1 2 3 4 5 6 | def login(_id): members = [ 'egoing' , 'k8805' , 'leezche' ] for member in members: if member = = _id: return True return False |
5.py
1 2 3 4 5 6 | import auth input_id = input ( "아이디를 입력해주세요.\n" ) if auth.login(input_id): print ( 'Hello, ' + input_id) else : print ( 'Who are you?' ) |
Ruby
auth.rb
1 2 3 4 5 6 7 8 9 10 11 12 | module Auth module_function() def login(_id) members = [ 'egoing' , 'k8805' , 'leezche' ] for member in members do if member == _id return true end end return false end end |
5.rb
1 2 3 4 5 6 7 8 | require_relative 'Auth' puts( "아이디를 입력해주세요" ) input_id = gets.chomp() if Auth.login(input_id) puts( 'Hello, ' +input_id) else puts( 'Who are you?' ) end |