*args, **kwargs
def 함수명(parameter):
로직코드
함수명(파라미터)
# 함수실행은 함수를 먼저 정의하고 나서 호출할 것!
>>> ## 리턴이 없는 함수
>>> def hello(world):
... print("Hello ", world)
...
>>> hello("Python!")
Hello Python!
>>>
>>> ## 리턴이 하나인 함수
>>> def hello_return(world):
... val = "Hello" + str(world)
... return val
...
>>> hello_return("Python!")
'HelloPython!'
>>>
>>> ## 다중리턴
>>> def func_mul(x):
... y1 = x * 1
... y2 = x * 2
... y3 = x * 3
... return y1, y2, y3
...
>>>
>>> val1, val2, val3 = func_mul(10)
>>> print(val1, val2, val3)
10 20 30
>>>
>>> ## 다중리턴인데 리스트로 리턴하기
>>> def func_mul2(x):
... y1 = x * 1
... y2 = x * 2
... y3 = x * 3
... return [y1,y2,y3]
...
>>> lt = func_mul2(100)
>>> print(lt)
[100, 200, 300]
>>>
*args
>>> def args_func(*args):
... print(args)
...
>>>
>>> args_func('a','b')
('a', 'b')
>>> args_func('a','b','c')
('a', 'b', 'c')
>>>
>>> def args_func2(*args):
... print(type(args))
...
>>> args_func2('a')
<class 'tuple'>
>>> args_func2('a','b')
<class 'tuple'>
>>>
>>> def args_func3(*args):
... for i,v in enumerate(args):
... print(i,v)
...
>>> args_func3('a','b','c')
0 a
1 b
2 c
>>>
**kwargs
>>> def kwargs_func(**kwargs):
... print(kwargs)
...
>>> kwargs_func(name1='kim',name2='park')
{'name1': 'kim', 'name2': 'park'}
>>>
>>>
>>> def kwargs_func2(**kwargs):
... print(kwargs)
...
>>> kwargs_func2(name1='kim')
{'name1': 'kim'}
>>>
>>>
>>> def kwargs_func3(**kwargs):
... for k,v in kwargs.items():
... print(k,v)
...
>>> kwargs_func3(name1='a', name2='b')
name1 a
name2 b
>>>
>>>
>>> def example_mul(arg1, arg2, *args, **kwargs):
... print(arg1, arg2, args, kwargs)
...
>>> example_mul(10,20)
10 20 () {}
>>>
>>> example_mul(10,20, 'park', 'kim', age1=24, age2=35)
10 20 ('park', 'kim') {'age1': 24, 'age2': 35}
>>>
>>> def nested_func(num):
... def func_in_func(num):
... print(num)
... print("in func")
... func_in_func(num + 10000)
...
>>> nested_func(10000)
in func
20000
>>>
>>>
>>> def nested_func2(num):
... def func_in_func(num):
... print("fun_in_func", num)
... print("in func")
... func_in_func(num + 10000)
...
>>> nested_func2(10000)
in func
fun_in_func 20000
>>>
>>>
def func_mul3(x : int) -> list
>>> def func_mul3(x : int) -> list : # 입력 x는 int이고 return은 list임을 알려주는 용도
... y1 = x * 100
... y2 = x * 200
... y3 = x * 300
... return [y1,y2,y3]
...
>>>
>>> func_mul3(5.0)
[500.0, 1000.0, 1500.0]
>>>
>>> func_mul3(5)
[500, 1000, 1500]
>>>
>>>
>>> def func_mul3(x : int) -> list :
... y1 = x * 100
... return y1
...
>>>
>>> func_mul3(5.0)
500.0
>>> type(func_mul3(5.0))
<class 'float'>
>>>
>>> func_mul3(5)
500
>>> type(func_mul3(5))
<class 'int'>
>>>
>>> def mul_10(num : int) -> int:
... return num * 10
...
>>>
>>> var_func = mul_10
>>> print(var_func)
<function mul_10 at 0x10b176320>
>>>
>>> print(type(var_func))
<class 'function'>
>>>
>>> print(var_func(10))
100
>>>
>>> # 람다
>>> lambda_mul_10 = lambda num: num * 10
>>>
>>> print(lambda_mul_10(10))
100
>>>
>>> def func_final(x,y,func):
... print(x * y * func(10))
...
>>> func_final(10,10,lambda_mul_10)
10000
>>>
>>>
>>> func_final(10,10,lambda x : x * 1000)
1000000
>>>