반응형
/******************************************************************************************************
-- Title : [PY3.3] 함수에서 변수 스코핑 룰 및 인수 모드, 재귀함수 호출
-- Reference : 빠르게 활용하는 파이썬3 프로그래밍
-- Key word : 파이썬 python variable 변수 스코핑 룰 scoping rule
******************************************************************************************************/
-- Title : [PY3.3] 함수에서 변수 스코핑 룰 및 인수 모드, 재귀함수 호출
-- Reference : 빠르게 활용하는 파이썬3 프로그래밍
-- Key word : 파이썬 python variable 변수 스코핑 룰 scoping rule
******************************************************************************************************/
#-- Local->Global->Built-In순으로 인식
>>> x=1
>>> def func(a):
return a+x
>>> x=1
>>> def func(a):
return a+x
>>> func(1)
2
>>> def func2(a):
x=2
return a+x
2
>>> def func2(a):
x=2
return a+x
>>> func2(1)
3
3
#-- 인수 모드
>>> def fimes(a=10, b=20):
return a*b
>>> def fimes(a=10, b=20):
return a*b
>>> fimes()
200
>>> fimes(5)
100
>>> fimes(5,6)
#-- 가변인수(튜플 형식)
>>> def union2(*ar):
res =[]
for item in ar:
for x in item:
if not x in res:
res.append(x)
return res
200
>>> fimes(5)
100
>>> fimes(5,6)
#-- 가변인수(튜플 형식)
>>> 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")
['H', 'A', 'M', 'E', 'G']
#-- 정의되지 않은 인수 처리(**사용하여 dictionary 형식으로 전달)
['H', 'A', 'M', 'E', 'G']
#-- 정의되지 않은 인수 처리(**사용하여 dictionary 형식으로 전달)
>>> def userURIBuilder(server, port, **user):
str="http://"+server+":"+port+"/?"
for key in user.keys():
str += key + "=" + user[key] + "&"
return str
str="http://"+server+":"+port+"/?"
for key in user.keys():
str += key + "=" + user[key] + "&"
return str
>>> userURIBuilder("test.com", "8080", id='userid', passed='12345678')
'http://test.com:8080/?passed=12345678&id=userid&'
#-- 재귀 함수 호출
>>> def factorial(x):
if x==1:
return 1
return x * factorial(x - 1)
'http://test.com:8080/?passed=12345678&id=userid&'
#-- 재귀 함수 호출
>>> def factorial(x):
if x==1:
return 1
return x * factorial(x - 1)
>>> factorial(10)
3628800
3628800
반응형