본문 바로가기

Work

Python - about builtin types

워낙 인간 언어에 가까운 semantic 언어이다 보니, 정확히 어떤 결과일지 오히려 예측이 안 된다.

 

a = 1
print(type(a))
if a == int:
    print("a == int")
if a is int:
    print("a is int")
if a is type(int):
    print("a is type(int)")
if type(a) == type(int):
    print("type(a) == type(int)")
if type(a) is type(int):
    print("type(a) is type(int)")
if type(a) is int:
    print("type(a) is int")
if type(a) == int:
    print("type(a) == int")

 

결과는 조금 의외다.

<class 'int'>
type(a) is int
type(a) == int

 

즉, type(a)와 데이터형 이름과 직접 비교해야 한다.

이게 사용자 정의 데이터형(파이썬에서라면 클래스 밖에 없겠지만)에도 동일하게 적용되는지 모르겠네. 아직 안 해 봤음.

 

'Work' 카테고리의 다른 글

사내 공모  (0) 2020.07.07
Python - ndarray 배열 1차원화  (0) 2020.07.06
Definitions of Defect, Error, Failure, Fault and Problem  (0) 2020.06.30
BMS Software Engineer Test  (0) 2020.06.11
Python - numpy arange vs. linspace  (0) 2020.05.29