-
Python 3.8 new featuresPython 2021. 2. 10. 08:55
1. 해마 연산자
fruits = { '사과': 20, '바나나' : 10 } # 기존 방식 count = fruits.get('바나나', 0) if count: print(f"make juice with {count}") else: print("need more fruits") # count 변수는 if 블록 안에서 사용되며, else에서는 사용하지 않으므로 # if 절에서 선언하여 가독성을 높임(if 절 이후 사용이 안될경우에만) if count := fruits.get('바나나', 0): # 왈러스 연산자(Walrus operator) print(f"make juice with {count=}") else: print("need more fruits") # 새로운 변수에 비교를 수행할 수 있음 단, 괄호 필요 if (count := fruits.get('바나나', 0)) > 10: print(f"make juice with {count=}") # f문자열, count=10 으로 출력됨 else: print("need more fruits")2. 위치, 키워드 매개 변수
## uid - 위치 기반 매개 변수 ## name, email - 위치 또는 키워드 기반, 반드시 필요함 ## phone_number, post_code - 키워드 기반 ## phone_number default 값이 없으므로 반드시 보내야함 def save_user_info(uid, /, name, email, *, phone_number, post_code=None, **kwargs): print(uid, name, email, phone_number, post_code, kwargs) #save_user_info(100) # wrong - name, email 필요 #save_user_info(100, 'daehan', 'gmail') # wrong - phone_number 필요 save_user_info(100, 'daehan', email='gmail', phone_number="01050174397") # / 이전에는 무조건 위치 기반 무조건 보내줘야함 # / 이후 * 사이에는 위치, 키워드 모두 가능하지만 # 위치 기반은 (default 값이 없으므로) 무조건 보내줘야함 # 키워드 기반은 default 값이 있기 때문에 필수 아님 # 위치 기반은 무조건 키워드 기반보다 먼저 와야함출처
'Python' 카테고리의 다른 글
OOP - 상속,변수, 초기화- Crawling/Custom Exceptions (0) 2022.01.13 [TEST] 2.테스트 코드 작성하기 (0) 2021.10.24 객체 지향 프로그래밍 - 크롤러 개발 (0) 2021.10.12 Python - Clean code - better/good/bad examples (0) 2021.02.10 Process / thread (0) 2020.11.04