Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 파이썬 확장자 분리
- 씨샵파일이름추출 #씨샵확장자제외한파일이름추출 #씨샵확장자추출
- 깃 브렌치 삭제
- 자바자료형 #Java자료형 #자바자료형종류 #Java자료형종류
- 파이썬 경로 분리
- VSCode #Prettier #Codeformatter
- 파일탐색기 브라우저처럼 사용
- 자바JDK설치
- 깃커밋메시지변경 #git커밋메시지변경 #깃커밋메시지수정 #git커밋메시지수정
- 파이썬가상환경생성 #파이썬가상환경활성화 #파이썬가상환경비활성화
- 파일탐색기 탭기능
- 자바개발환경구축 #자바개발환경구축하기 #자바설치
- 파이썬리스트 #파이썬딕셔너리 #파이썬리스트안딕셔너리데이터 #파이썬리스트딕셔너리
- 파이썬 파일명과 확장자 분리
- 리눅스 #리눅스마운트 #마운트 #mount #마운트개념
- SOLID #객체지향설계5원칙 #DIP #ISP #SRP #LSP #OCP
- 데이터베이스 #데이터베이스란 #DBMS #SQL
- 파일탐색기 탭기능 사용하기
- 파이썬 경로추출
- 깃stash #gitstash #새로운파일stash
- 파이썬버전확인하기 #파이썬버전조회
- 씨샵 #함수에서여러개값내보내기 #함수에서여러개의return받기 #함수여러개값리턴
- 파이썬코딩가이드 #파이썬PEP8 #파이썬PEP8스타일가이드
- 모달 #Modal #모달리스 #Modaless
- gRPC #gRPC튜토리얼
- 이클립스프로젝트위치이동 #이클립스프로젝트위치변경 #이클립스프로젝트로컬위치변경 #이클립스프로젝트로컬위치이동
- python #파이썬 #dictionary #딕셔너리 #del #pop()
- 씨샵 #foreach #foreachindex
- gRPC프로젝트생성 #gRPCC# #gRPC서버클라이언트 #gRPC튜토리얼 #gRPCTutorial
- DB #데이터베이스 #테이블 #로우 #컬럼 #Row #Column #Table
Archives
- Today
- Total
개발자로 살아남기
[Python] 파이썬 데이터 타입 확인하기(type(), isinstance()) 본문
파이썬에서 데이터의 타입을 확인하는 방법을 알아보자.
1. type() : 입력된 인자의 타입 정보를 return한다.
# int
print(type(1234))
# float
print(type(1234.1234))
# str
print(type("str"))
# list
print(type([1, 2, 3, 4]))
# dict
print(type({'1' : 2}))
# tuple
print(type((1, 2, 3, 4)))
# function
def type_function():
pass
print(type(type_function))
결과
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'dict'>
<class 'tuple'>
<class 'function'>
2. isinstance() : 입력된 데이터와 체크할 타입을 인자로 받아, 타입이 일치하는지 확인한다.
타입이 일치하면 True, 일치하지 않으면 False를 return한다.
주로 타입별로 분기를 탈 때 사용한다.
# int
print(isinstance(1234, int))
# float
print(isinstance(1234.1234, float))
# str
print(isinstance("str", str))
# list
print(isinstance([1, 2, 3, 4], list))
# dict
print(isinstance({'1' : 2}, dict))
# set
print(isinstance((1, 2, 3, 4), tuple))
# int or list이면
print(isinstance([1, 2, 3, 4], (int, list)))
print(isinstance([1, 2, 3, 4], (int, float)))
결과
True
True
True
True
True
True
True
False
마지막과 같이 입력된 타입 중 하나라도 일치하면 True를 반환한다.
if isinstance(123, int):
print('Type은 int입니다')
if isinstance(123.123, float):
print('Type은 float입니다')
if isinstance('str', str):
print('Type은 str입니다')
Type은 int입니다
Type은 float입니다
Type은 str입니다
True, False를 반환하는 것을 이용하여 if문과 함께 사용하여 타입 별로 분기를 타게 활용할 수 있다.
아래와 같이 class에도 활용할 수 있다.
class TypeCheck:
print("")
type_check = TypeCheck()
print(type(type_check))
print(isinstance(type_check, TypeCheck))
결과
<class '__main__.TypeCheck'>
True
'프로그래밍 > Python' 카테고리의 다른 글
[Python] 리스트 안 딕셔너리에서 원하는 데이터 얻어오기 (0) | 2023.10.27 |
---|---|
[Python] 파이썬 스타일 가이드(PEP 8) (0) | 2023.08.12 |
[Python] 사용 중인 파이썬 버전 확인하기 (0) | 2023.08.10 |
[Python] 파이썬 경로 분리하기(파일명만 추출, 파일 확장자만 추출, 경로 추출, 경로와 파일명을 분리 등) (0) | 2023.05.20 |
[Python] 파이썬 폴더 생성, 폴더 자동 생성, 폴더 있는지 확인하기(os.mkdir, os.makedirs, os.path.isdir) (0) | 2023.03.16 |