티스토리 뷰
-. 기본 인수 값
함수 지정시 인수의 기본값을 지정
>>> def multi(a=10,b=20):
return a + b
>>> multi() #인수를 넣지 않으면 기본 값 사용
30
>>> multi(1) #인수를 넣으면 인수 사용
21
def multi(a=20,b) << 사용불가
※기본 인수값을 사용하는 인수 뒤에 기본 값이 없는 인수가 올수 없음.
-. 키워드 인수
인수이름을 명시하여 순서가 바꿔서 사용
>>> def cURL(Server,Port):
str = 'http://' + Server + ':' + Port
return str
>>> cURL('test.com','8080')
>>> cURL(Port = '8080',Server = 'test.com')
※키워드 인수 이후에는 순서에 의한 인수 매칭을 할수 없음
-. 가변 인수 리스트
‘ * ’ 붙여서 사용
인수의 개수가 정해지지 않은 가변인수
전달받은 인수는 튜플형태로 처리
>>> def test(*args):
print(args)
print(type(args))
>>> test(1,2,3)
(1, 2, 3)
<class 'tuple'>
가변 인수 리스트를 이용하여 합집합을 구하는 함수
>>> def union2(*ar):
res = []
for item in ar:
for x in item:
if not x in res:
res.append(x)
return res
>>> union2("ham","egg","spam")
['h', 'a', 'm', 'e', 'g', 's', 'p']
-. 정의되지 않은 인수 처리하기
‘ ** ’ 붙여서 사용
정의되지 않은 인수를 받는 부분은 인수들중 가장 마지막에 와야함
정의되지 않은 인수를 사전형식으로 전달 받음
>>> def CreFruit(country,city,**fruit):
str = "국가: " + country + "/도시: " + city + "/"
for key in fruit.keys(): #fruit로 받아들인 사전형식의 인수를 국가와 도시뒤에 붙여줌
str += key + "=" + fruit[key] + "/"
return str
>>> CreFruit("korea","Seoul",apple='red',banana='yellow')
'국가: korea/도시: Seoul/apple=red/banana=yellow/'
'Python' 카테고리의 다른 글
Python, 재귀적 함수 호출 (0) | 2016.04.21 |
---|---|
Python, 함수 lambda (0) | 2016.04.21 |
Python, 함수_스코핑룰 (0) | 2016.04.21 |
Python, 함수_인수 전달 (0) | 2016.04.21 |
Python, 함수 (0) | 2016.04.21 |
- Total
- Today
- Yesterday
- 파이썬
- tkinter command & bind [명령어묶기와 사건묶기] Python
- Java
- Private
- command
- vba
- checkbutton
- activebackground
- IdClass
- tkinter
- JPA
- apache
- indicatoron
- highlightthickness
- Module
- Excel
- Linux
- 리눅스
- Python
- disabledforeground
- FetchType
- 폼
- onetomany
- activeforeground
- 상수
- Composite Key
- highlightbackground
- ManyToOne
- borderwidth
- fetch join
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |