

class Name(object):
class Book(object):
author = ""
title = ""
publisher = ""
date = ""
book = Book()
book.author = 'Suan"
print(book.author)
book.title = 'Python Programming'
print(book.title)
Out:
Suan
Python Programming
메소드
print_info(self)print_info(self)에서 self는 실제적으로 book 인스턴스를 의미self 생략 가능class Book(object):
author = ""
title = ""
publisher = ""
date = ""
def print_info(self):
print("Author:", self.author)
print("Title:", self.title)
book = Book()
book.author = 'Suan"
book.title = 'Python Programming'
book.print_info()
Out:
Author: Suan
Title: Python Programming

Book 인스턴스 속성
b1에서 속성을 활용class Book(object):
author = ""
title = ""
publisher = ""
date = ""
def print_info(self):
print("Author:", self.author)
print("Title:", self.title)
print("Publisher:", self.publisher)
print("Date:", self.date)
b1 = Book()
b1.author = 'Suan'
b1.title = 'Python Programming'
b1.publisher = 'Colab'
b1.date = '2022'
b1.print_info()
Out:
Author: Suan
Title: Python Programming
Publisher: Colab
Date: 2022

Book 클래스 속성
class Book(object):
author = ""
title = ""
publisher = ""
date = ""
def print_info(self):
print("Author:", self.author)
print("Title:", self.title)
print("Publisher:", self.publisher)
print("Date:", self.date)
b1 = Book()
Book.author = 'Suan'
Book.title = 'Python Programming'
Book.publisher = 'Colab'
Book.date = '2022'
b1.print_info()
Out:
Author: Suan
Title: Python Programming
Publisher: Colab
Date: 2022
b1 대신 클래스명으로 접근하였는데도 똑같이 출력된다.

Book 인스턴스 속성과 클래스 속성
class Book(object):
author = ""
title = ""
publisher = ""
date = ""
count = 0
def print_info(self):
print("Author:", self.author)
print("Title:", self.title)
print("Publisher:", self.publisher)
print("Date:", self.date)
b1 = Book()
Book.count += 1
b1.author = 'Suan'
b1.title = 'Python Programming'
b1.publisher = 'Colab'
b1.date = '2022'
b1.print_info()
print("Number of Books", str(Book.count)
Out:
Author: Suan
Title: Python Programming
Publisher: Colab
Date: 2022
Number of Books: 1
| 매직 메소드 | 설명 |
|---|---|
| __init__ | 객체 초기화를 위해 클래스 생성시 호출되는 동작을 정의 |
| __str__ | 클래스의 인스턴스에서 str()이 호출될 때의 동작을 정의 |
| __repr__ | 클래스의 인스턴스에서 repr()이 호출될 때의 동작을 정의 |
| __new__ | 객체의 인스턴스화에서 호출되는 첫 번째 메소드 |
| __del__ | 객체가 소멸될 때 호출되는 메소드 |
| __getattr__ | 존재하지 않는 속성에 엑세스하려고 시도할 때 행위를 정의 |
| __setattr__ | 캡슐화를 위한 방법 정의 |
| __add__ | 두 인스턴스의 더하기가 일어날 때 실행되는 동작 정의 |
| __lt__, __le__, __gt__, __ge__, __eq__, __ne__ | 인스턴스 간의 <, <=, >,, >=, ==, != 비교 메소드 |
init() 메소드를 이용하여 클래스의 속성들을 초기화class Book(object):
count = 0
def __init__(self, author, title, publisher, data):
self.author = author
self.title = title
self.publisher = publisher
self.date = date
def print_info(self):
print("Author:", self.author)
print("Title:", self.title)
print("Publisher:", self.publisher)
print("Date:", self.date)
book = Book('Suan', 'Python Programming', 'Colab', '2020')
book.print_info()
print("Number of Books", str(Book.count)
Out:
Author: Suan
Title: Python Programming
Publisher: Colab
Date: 2022
Number of Books: 2
str() 메소드를 이용하여 인스턴스 출력class Book(object):
count = 0
def __init__(self, author, title, publisher, data):
self.author = author
self.title = title
self.publisher = publisher
self.date = date
def __str__(self):
return ("Author:", self.author + \
"\nTitle:", self.title + \
"\nPublisher:", self.title + \
"\nDate:", self.date)
book = Book('Suan', 'Python Programming', 'Colab', '2022')
print(book)
print('Number of Boos:', str(Book.count))
Out:
Author: Suan
Title: Python Programming
Publisher: Colab
Date: 2022
Number of Books: 2
class SubClass(SuperClass):class SuperClass(object):
pass
class SubClass(SuperClass):
pass
class를 상속하는 방법은 class(상속할 클래스)이다. 생각보다 간단하다!
SuperClass로부터 SubClass1와 SubClass2가 클래스 상속method()를 정의Subclass1의 method()는 SuperClass의 추상 메소드를 오버라이딩class SuperClass(object):
def method(self):
pass
class SubClass1(SuperClass):
def method(self):
print("Method Overriding")
class Subclass2(SuperClass):
pass
sub1 = SubClass1()
sub2 = SubClass2()
sub1.method()
sub2.method()
Out:
Method Overriding
출력값으로 'Method Overriding'이 나왔습니다.
이 결과는 sub1이 호출한 결괏값입낟.
그런데 SubClass2를 받은 sub2의 경우 호출할 게 아무 것도 없는데 error가 나지 않습니다.
그 이유는 SuperClass를 상속 받아서 이미 method 정의를 그대로 가져왔기 때문에 호출될 수 있었습니다.
Vehicle 클래스를 상속받아 Car 클래스와 Truck 클래스 생성Car 클래스와 Truck 클래스는 up_speed 메소드를 오버라이딩Car 클래스는 속도가 240 초과되면 240으로 조정Truck 클래스는 속도가 180 초과되면 180으로 조정class Vehicle(object):
speed = 0
def up_speed(self, value):
self.speed += value
def down_speed(self, value):
self.speed -= value
def print_speed(self):
print("Speed:", str(self.speed))
class Car(Vehicle):
def up_speed(self, value):
self.speed += value
if self.speed > 240: self.speed = 240
class Truck(Vehicle):
def up_speed(self, value):
self.speed += value
if self.speed > 180: self.speed = 180
car = Car()
car.up_speed(300)
car.print_speed()
truck = Truck()
truck.up_speed(200)
truck.print_speed()
Out:
Speed: 240
Speed: 180
🎥 유튜브 파이썬 쉽게 배우기 - 08 객체와 클래스
📸 이미지는 ppt 수작업하였습니다. ✨