-
type annotationpython 기초 2023. 1. 24. 12:13728x90
python / matlab과 같은 동적 타입 언어를 주로 쓰다보면,
만들때는 응당 그러려니 하고 만들었던 함수가 나중에는 헷갈리는 경우가 많다.
주석도 중요하지만, type annotation 또한 중요해보인다.
def int_mul(a:int,b:int): return a*b def float_mul(a,b): return a*b print(int_mul.__annotations__) print(float_mul.__annotations__)
위의 int_mul 함수와 같이 입력 변수 뒤에 :type 으로 input에 대한 annotation을 해주면 된다.
이렇게 되면 아래에서 함수명.__annotations__ 를 통해 호출해서 각 변수의 type을 알수 있다.
int_mul의 type annotaiton은 잘 출력되었다. float_mul은 annotation을 해주지 않았으므로 비어있는 결과가 나온다.
협업시 중요한 기능 중 하나라 할 수 있다.습관을 들이자.
728x90'python 기초' 카테고리의 다른 글
pandas iloc loc 차이 (0) 2023.01.25 python repr eval 사용 (0) 2023.01.24 python generator (0) 2023.01.23 python iterator (0) 2023.01.23 python closure / decorator (0) 2023.01.23