Python에서 객체(object)란, 클래스(class)라는 설계도를 기반으로 만들어진 구체적인 실체(instance)를 뜻한다.
클래스(class):
자동차라는 클래스를 만든다면, 여기에는 자동차가 가질 수 있는 속성(예: 색깔, 모델)과 기능(예: 달리기, 멈추기)이 정의되어 있습니다.객체(object):
자동차 클래스를 이용해서 빨간 스포츠카, 검은 SUV와 같은 각각의 자동차를 만들 수 있습니다. 이 각각이 객체입니다.# 클래스 정의
class Car:
def __init__(self, color, model):
self.color = color # 자동차의 색상
self.model = model # 자동차의 모델
def drive(self):
print(f"The {self.color} {self.model} is driving!")
# 객체 생성
car1 = Car("red", "sedan") # 빨간색 세단
car2 = Car("black", "SUV") # 검은색 SUV
# 객체 사용
car1.drive() # The red sedan is driving!
car2.drive() # The black SUV is driving!
Car는 클래스입니다. 자동차가 어떻게 만들어질지 설계도를 제공합니다.car1과 car2는 Car 클래스에서 생성된 객체입니다.car1은 빨간색 세단이고, car2는 검은색 SUV입니다.color, model)과 메서드(drive)를 사용하지만, 각각의 객체는 서로 다른 속성을 가질 수 있습니다.