python/기본
-
Exceptionpython/기본 2022. 4. 29. 22:45
python exception hierachy https://docs.python.org/3/library/exceptions.html#exception-hierarchy l = [1, 2, 3] i = 5 try: l[i] except IndexError as e: print(f"Don't worry: {e}") except NameError as e: print(e) except Exception as e: print(f'other: {e}') else: print('done') # 예외가 발생하지 않고 잘 수행되었을 때 실행되고, finally 전에 무언가를 수행하고 싶을 때 사용 한다. finally: print("run always") try: result =4/0 except: print("e..
-
Namespace & Scopepython/기본 2022. 4. 29. 22:33
animal = 'cat' def f(): # print(animal) # 아래에서 animal 이 정의되지 않으면 'cat' 으로 출력, 아래에서 animal 이 정의되면 에러 발생 animal = 'dog' print(animal) # dog def g(): global animal print(animal) # cat print('local', locals()) def h(): """Test func doc""" print(h.__name__) # h print(h.__doc__) print('global:', globals())
-
Generatorpython/기본 2022. 4. 29. 21:59
l = ['a', 'b', 'c'] for i in l: print(i) #################################################################### # Generator def greeting(): yield 'a' yield 'b' yield 'c' # for g in greeting(): # print(g) g = greeting() print(next(g)) # a print(next(g)) # b print(next(g)) # c # print(next(g)) # StopIteration #################################################################### t = (1, 2, 3, 4, 5) t2..
-
Lambdapython/기본 2022. 4. 29. 21:44
l = ['Mon', 'tue', 'Wed', 'Thu', 'fri', 'sat', 'Sun'] def change_words(words, func): for word in words: print(func(word)) def sample_func(word): return word.capitalize() # sample_func = lambda word: word.capitalize() # 마치 scala 의 map 처럼 처리? change_words(l, sample_func) change_words(l, lambda word: word.capitalize()) ''' Mon Tue Wed Thu Fri Sat Sun '''
-
Decorator / contextmanagerpython/기본 2022. 4. 29. 20:24
def add_num(a, b): return a + bprint('start')r = add_num(1, 2)print('end')print(r)##################################################################### AOP 처럼 ...def print_info(func): def wrapper(*args, **kwargs): print('start') result = func(*args, **kwargs) print('end') return result return wrapperdef add_num(a, b): return a + b# 첫번째 사용 방법f = print_info(..
-
Closurepython/기본 2022. 4. 29. 18:44
def outer(a, b): def inner(): return a + b return inner f = outer(1, 2) r = f() # 이 시점에 1 + 2 가 실행됨, lazy evaluation? print(r) # 3 #################################################################### # pi 값을 변경하면서 계산하고 싶다.. def circle_area_func(pi): def circle_area(radius): return pi * radius * radius return circle_area cal = circle_area_func(3.14) cal2 = circle_area_func(3.141592) print(cal(10)..
-
Function, kwargspython/기본 2022. 4. 29. 18:37
# num: int = 10 def add_num(a: int, b: int) -> int: """add two int :param a: first param :param b: second param :return: sum value """ return a + b print(help(add_num)) r = add_num(1, 2) print(r) s = add_num('X', 'Y') print(s) # XY working!!! #################################################################### def menu(x, y, z='Z'): print(x, y, z) menu(x='a', y='b') #############################..
-
range, enumerate, zippython/기본 2022. 4. 29. 18:03
for i in range(5): print(i) # 0 1 2 3 4 for i in range(2, 10, 3): print(i) # 2 5 8 for _ in range(10): # under score 로 실제 값을 직접 활용하지 않음을 표시 print('hello') #################################################################### for i, x in enumerate(['A', 'B', 'C']): print(i, x) ''' 0 A 1 B 2 C ''' #################################################################### days = ['Mon', 'Tue', 'Wed'] frui..