클래스와 객체 만들기
첫 번째 Python 클래스를 만듭니다.
클래스와 객체 만들기은(는) CoddyKit의 무료 Python For Kids 강의입니다. 이것은 5개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Python For Kids 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Python For Kids 강의에는 총 5개의 강의가 포함되어 있습니다.
클래스와 객체 만들기
객체 지향 프로그래밍(OOP) 수업에 다시 오신 것을 환영합니다! 오늘은 Python에서 직접 클래스와 객체를 만드는 방법을 알아보겠습니다. 클래스와 객체를 사용하면 관련된 속성과 동작을 함께 묶어 코드를 체계적으로 구성할 수 있습니다.

클래스란?
클래스는 객체를 만들기 위한 설계도와 같습니다. 클래스에서 만들어진 객체가 가지게 될 속성(속성)과 동작(메서드)을 정의합니다.
- 속성: 객체의 특징입니다(예: 색상, 크기).
- 메서드: 객체가 수행할 수 있는 동작입니다(예: 이동, speak).
클래스를 요리법으로, 객체를 그 요리법으로 만든 요리라고 생각해 보세요.
Python에서 클래스 정의하기
Python에서는 class 키워드 뒤에 클래스 이름과 콜론을 작성하여 클래스를 정의할 수 있습니다. 클래스 내부에는 속성과 메서드를 정의합니다.
문법:
class ClassName:
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
def method_name(self):
# Code for the method
pass
__init__ 메서드
__init__ 메서드는 Python 클래스에서 사용하는 특별한 메서드입니다. 클래스에서 새 객체를 만들 때 호출되며 객체의 속성을 초기화하는 데 사용됩니다.
예시:
# Defines the Person class with name and age attributes
class Person:
def __init__(self, name, age):
self.name = name # Sets the name of the person
self.age = age # Sets the age of the person
클래스에서 객체 만들기
클래스를 정의한 후에는 클래스 이름을 함수처럼 호출하여 해당 클래스에서 객체(인스턴스)를 만들 수 있습니다.
예시:
# Creates an object of the Person class
person1 = Person("Alice", 12)
# Accesses the object's attributes
print(person1.name) # Output: Alice
print(person1.age) # Output: 12
클래스에 메서드 추가하기
메서드는 클래스 내부에 정의된 함수로, 해당 클래스에서 만들어진 객체의 동작을 설명합니다.
예시:
# Defines the Person class with a greet method
class Person:
def __init__(self, name, age):
self.name = name # Sets the name of the person
self.age = age # Sets the age of the person
def greet(self):
print("Hello, my name is " + self.name + "!") # Prints a greeting message
# Creates an object of the Person class
person1 = Person("Bob", 15)
# Calls the greet method of the object
person1.greet() # Output: Hello, my name is Bob!
예제 프로그램: 간단한 클래스 만들기
클래스와 객체의 작동 방식을 이해하기 위해 속성과 메서드를 가진 간단한 Car 클래스를 만들어 보겠습니다.
코드:
# Defines the Car class
class Car:
def __init__(self, make, model, color):
self.make = make # Sets the make of the car
self.model = model # Sets the model of the car
self.color = color # Sets the color of the car
def display_info(self):
print("Car Make:", self.make) # Prints the make of the car
print("Car Model:", self.model) # Prints the model of the car
print("Car Color:", self.color) # Prints the color of the car
def paint(self, new_color):
self.color = new_color # Changes the color of the car
print("The car has been painted to", self.color) # Confirms the color change
# Creates an object of the Car class
my_car = Car("Toyota", "Corolla", "Red")
# Calls the display_info method
my_car.display_info()
# Output:
# Car Make: Toyota
# Car Model: Corolla
# Car Color: Red
# Calls the paint method to change the car's color
my_car.paint("Blue")
# Output: The car has been painted to Blue
# Displays the updated car information
my_car.display_info()
# Output:
# Car Make: Toyota
# Car Model: Corolla
# Car Color: Blue
인스턴스 변수와 클래스 변수 비교
Python 클래스에서 변수는 다음과 같이 분류할 수 있습니다.
- 인스턴스 변수: 각 객체 인스턴스에 고유한 변수입니다.
- 클래스 변수: 해당 클래스의 모든 인스턴스가 공유하는 변수입니다.
두 종류의 변수를 모두 사용하는 방법을 살펴보겠습니다.
# Defines the Student class with a class variable
class Student:
school = "XYZ High School" # Class variable
def __init__(self, name):
self.name = name # Instance variable
def display_info(self):
print(self.name + " attends " + Student.school) # Prints student info
# Creates two objects of the Student class
student1 = Student("Alice")
student2 = Student("Bob")
# Calls the display_info method for both students
student1.display_info()
student2.display_info()
훌륭합니다!
Python에서 클래스와 객체를 만들고, 속성과 메서드를 정의하여 코드를 효과적으로 구성하는 방법을 배웠습니다. 클래스와 객체를 이해하는 것은 객체 지향 프로그래밍(OOP)의 기본이며, 이를 통해 더 복잡하고 재사용 가능한 프로그램을 만들 수 있습니다. 직접 클래스를 만들고 다양한 속성과 메서드를 실험하며 계속 연습해 보세요!

자주 묻는 질문
“클래스와 객체 만들기” 강의는 무료인가요?
네 — “클래스와 객체 만들기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Python For Kids 강의 전체를 잠금 해제할 수 있습니다. Python For Kids 강의에는 총 5개의 강의가 포함되어 있습니다.
“클래스와 객체 만들기”에서 뭘 배우나요?
첫 번째 Python 클래스를 만듭니다. 브라우저에서 직접 실행하는 실습 코드로 Python For Kids을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Python For Kids을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Python For Kids은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 5개 중 2번째 강의입니다.
“클래스와 객체 만들기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Python For Kids 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Python For Kids 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 객체 지향 프로그래밍 소개
- 클래스와 객체 만들기
- 상속
- 캡슐화
- 다형성