커뮤니티

고용노동부, 산업인력공단과 함께하는 강원도 유일한 기업중심 IT전문교육기관 ICT융합캠퍼스만의 특별한교육입니다.
공인 IT숙련기술인의 다양한 접근방법으로 전문가다운 실무교육을 받을 수 있습니다.

Category

교육강좌

언어 Python & Ruby - 모듈

페이지 정보

작성자 관리자 댓글 0건 조회 2,610회 작성일 20-06-10 13:59

본문

모듈

모듈이란?

내장모듈

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

ideone.com

Ruby

1
puts(Math.sqrt(16))
1
4.0

ideone.com

모듈에 없을 때

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 | Ruby 

모듈의 도입 - 파이썬

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
  • 트위터로 보내기
  • 페이스북으로 보내기
  • 구글플러스로 보내기

답변목록

등록된 답변이 없습니다.