목록전체 글 (305)
RUBY
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/4ENsY/btrVzyBK9ek/7ERIN5zYkiST9s6thSMIaK/img.png)
모듈 모듈이란? 프로그램을 구성하는 구성 요소로, 관련된 데이터와 함수를 하나로 묶은 단위를 의미합니다. 보통 하나의 소스 파일에 모든 함수를 작성하지 않고, 함수의 기능별로 따로 모듈을 구성합니다. 이러한 모듈을 합쳐 하나의 파일로 작성하는 방식으로 프로그램을 만들게 됩니다. 파이썬 모듈은 내부모듈, 외부 모듈, 사용자 모듈로 구분할 수 있다. 내부 모듈 → 파이썬 설치 시 기본적으로 사용할 수 있는 모듈 외부 모듈 → 별도 설치 후 사용할 수 있는 모듈 사용자 모듈 → 사용자가 직접 만든 모듈 import import 키워드를 이용해서 모듈을 임포트한다. as 키워드를 이용해서 모듈 이름을 단축 시킬 수 있다. from from ~ as 키워드를 이용해서 모듈의 특정 기능만 사용할 수 있다.
lambda함수 lambda lambda 키워드를 이용하면 함수 선언을 보다 간단하게 할 수 있다. def calculator(n1, n2): return n1 + n2 returnValue = calculator(10, 20) print(f'returnValue: {returnValue}') calculator = lambda n1, n2: n1 + n2 returnValue = calculator(10, 20) print(f'returnValue: {returnValue}')
중첩함수 중첩함수 함수안에 또 다른 함수가 있는 형태이다. def out_funtion(): print('out_function called!!') def in_funtion(): print('in_function called!!') in_funtion() out_funtion() out_function called!! in_function called!! 내부 함수를 함수 밖에서 호출할 수 없다. def out_funtion(): print('out_function called!!') def in_funtion(): print('in_function called!!') in_funtion() in_funtion() NameError: name 'in_funtion' is not defined. Did ..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/CPjW2/btrVG0CVJ0w/LuHKzk2xsrSosGAVANviwK/img.png)
지역변수와 전역변수 지역 변수 함수 안에서 선언된 변수로 함수 안에서만 사용 가능하다. def printNumbers(): num_in = 20 print(f'num_in: {num_in}') printNumbers() num_in: 20 def printNumbers(): num_in = 20 print(f'num_in: {num_in}') print(f'num_in: {num_in}') NameError: name 'num_in' is not defined 전역 변수 함수 밖에 선언된 변수로 어디에서나 사용은 가능하지만 함수 안에서 수정할 수는 없다. global 키워드 global을 사용하면 함수 안에서도 전역변수의 값을 수정할 수 있다.
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/cXSZqn/btrVAuyCkhZ/EG2K5ATNcA8f41RLTwbdX0/img.png)
데이터 반환 함수 실행 결과 반환 return 키워드를 이용하면 함수 실행 결과를 호출부로 반환할 수 있다. 함수가 return을 만나면 실행을 종료한다.
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/lolcA/btrVzKvaIxh/p52XHK4OjKoTKkUrmM1Kmk/img.png)
인수와 매개변수 인수와 매개변수 함수 호출 시 함수에 데이터를 전달할 수 있다. 인수와 매개변수 개수는 일치해야 한다. 매개변수 개수가 정해지지 않은 경우 '*'를 이용한다.
함수 내에서 또 다른 함수 호출 또 다른 함수 호출 함수 내에서 또 다른 함수를 호출할 수 있다. def fun1(): print('fun1 호출!') fun2() def fun2(): print('fun2 호출!') fun3() def fun3(): print('fun3 호출!') fun1() fun1 호출! fun2 호출! fun3 호출! pass 사용 pass를 이용해서 실행문을 생략할 수 있다. def printTodayWeather(): pass def printTomorrowWeather(): pass printTodayWeather() printTomorrowWeather()