python은 객체로 이루어져 있다?

hodu·2022년 10월 2일
0

python

목록 보기
10/17

파이썬은 모든 것이 다 객체다.
변수, 리스트부터 시작해서 클래스도 객체임

그럼 객체는 무엇일까?
객체는 어떠한 속성값과 행동을 가지고 있는 데이터다.

즉, 우리가 숫자형을 만든다면
숫자형이라는 클래스를 통해서 생성이 된다.

print(type(1))
# <class 'int'>

문자형도 마찬가지로 문자형이라는 클래스를 통해서 생성이 된다.

즉 객체의 타입은 객체를 만든 클래스라고 볼 수 있는데, 모든 객체들은 이 클래스를 통해서 생성이 된다.

그럼 클래스란 무엇이며 왜 쓸까?
클래스는 "새로운 타입"을 만들기 위해 사용하며, 객체의 속성과 행동을 한꺼번에 묶어서 정의하기 위해 사용한다.

저번 포스팅(클래스와 인스턴스의 차이) 에서 작성하였다시피 클래스는 객체를 만드는 틀이고
클래스로 객체를 생성하게 되면 생성된 객체들은 해당 속성과 메서드를 가질 수 있게 된다.

그럼 각자의 객체가 가진 속성은 어떻게 확인할까?

dir('int')

다음과 같이 작성하여 실행하면 int의 속성과 메서드들을 확인할 수있다.

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
profile
안녕 세계!

0개의 댓글