클래스 이름: Book
속성
class Book(object): author = "" title = "" publisher = "" date = ""
book = Book() book.author = "Minjun" print(book.author) book.title = "Python Programming for machine Learning" print(book.title) #Minjun #Python Programming for machine Learning
메소드
print_info(self)
self
가 있어야만 실제로 인스턴스가 사용할 수 있는 메소드로 선언print_info(self)
에서 self
는 실제적으로 book 인스턴스
를 의미self
생략 가능class Book(object): author = "" title = "" publisher = "" date = "" def print_info(self): #book instance print("Author:", self.author) print("Title:", self.title)
book = Book() book.author = "Minjun" book.title = "Python Programming for machine Learning" book.print_info() #Author: Minjun #Title: Python Programming for machine Learning
Book 클래스
에서 생성된 인스턴스 b1에서 속성을 활용
class Book(object): author = "" title = "" publisher = "" date = "" def print_info(self): #book instance print("Author:", self.author) print("Title:", self.title) print("Publisher:",self.publisher) print("Date:", self.date)
b1 = Book() b1.author = "Minjun" b1.title = "Python Programming for web" b1.publisher = "Danny Prok" b1.date = "2024" b1.print_info() #Author: Minjun #Title: Python Programming for web #Publisher: Danny Prok #Date: 2024
클래스 속성은 클래스 자체에서 사용되는 속성
Book 클래스 자체에서 사용되는 속성
class Book(object): author = "" title = "" publisher = "" date = "" def print_info(self): #book instance print("Author:", self.author) print("Title:", self.title) print("Publisher:",self.publisher) print("Date:", self.date)
b1 = Book() Book.author = "Minjun" Book.title = "Python Programming for web" Book.publisher = "Danny Prok" Book.date = "2024" b1.print_info() #Author: Minjun #Title: Python Programming for web #Publisher: Danny Prok #Date: 2024
인스턴스 속성과 클래스 속성을 목적에 맞도록 나누어서 활용
인스턴스 속성
클래스 속성
class Book(object): author = "" title = "" publisher = "" date = "" count = 0 def print_info(self): #book instance print("Author:", self.author) print("Title:", self.title) print("Publisher:",self.publisher) print("Date:", self.date)
b1 = Book() Book.count += 1 b1.author = "Minjun" b1.title = "Python Programming for web" b1.publisher = "Danny Prok" b1.date = "2024" b1.print_info() print("Numbers of Books : ", str(Book.count)) #Author: Minjun #Title: Python Programming for web #Publisher: Danny Prok #Date: 2024 #Numbers of Books : 2
__init__()
__init__() 메소드
를 이용하여 클래스의 속성들을 초기화
class Book(object): count = 0 def __init__ (self, author, title, publisher, date): self.author = author self.title = title self.publisher = publisher self.date = date Book.count += 1 def print_info(self): print("Author:", self.author) print("Title:", self.title) print("Publisher:",self.publisher) print("Date:", self.date)
book = Book("Minjun", "Python Programming for ML", "ML Project", "2024" ) book.print_info() print("Number of Books", str(Book.count)) #Author: Minjun #Title: Python Programming for ML #Publisher: ML Project #Date: 2024 #Number of Books 3
__str__()
__str__() 메소드
를 이용하여 인스턴스 출력
class Book(object): count = 0 def __init__ (self, author, title, publisher, date): self.author = author self.title = title self.publisher = publisher self.date = date Book.count += 1 #def __str__(self): #2020 버전에서는 가능 #return ("Author:", self.author + \ #"\nTitle:", self.title + \ #"\nPublisher:",self.publisher + \ #"\nDate:", self.date) def __str__(self): #2024 버전에서는 이 기능을 지원하는 것 같다. return f"{self.author} +, {self.title}, {self.publisher}, {self.date}, {Book.count}"
book = Book("Minjun", "Python Programming for ML", "ML Project", "2024" ) print(book) print("Number of Books", str(Book.count)) #Minjun, Python Programming for ML, ML Project, 2024, 1 #Number of Books 1
Line 클래스
class Line(object): length = 0 # def __init__(self, length): self.length = length print(self.length, "길이의 선 생성") # def __del__(self): print(self.length, "길이의 선 제거") # def __repr__(self): return str(self.length) # def __add__(self, other): return self.length + other.length # def __It__(self, other): return self.length < other.length # def __le__(self, other): return self.length <= other.length # def __gt__(self, other): return self.length > other.length # def __ge__(self, other): return self.length >= other.length # def __eq__(self, other): return self.length == other.length # def __ne__(self, other): return self.length != other.length
l1 = Line(10) print(l1) # l2 = Line(20) print(l2) # print("선의 합:", l1+l2) # if l1 < l2: print(l1, '<', l2) elif l1 <= 12: print(l1, '<=', l2) elif l1 > l2: print(l1, '>', l2) elif l1 >= l2: print(l1, '>=', l2) elif l1 == l2: print(l1, '==', l2) elif l1 != l2: print(l1, '!=', l2) else: pass # del(l1) del(l2) #10 길이의 선 생성 #10 #20 길이의 선 생성 #20 #선의 합: 30 #10 < 20 #10 길이의 선 제거 #20 길이의 선 제거 #10 길이의 선 제거 #20 길이의 선 제거
가시성 예제
__items 속성
은 Box 객체 외부에서 보이지 않도록 캡슐화와 정보 은닉이 가능
외부에서 __items 속성
에 접근하면 속성 오류 발생
class Box(object): def __init__(self, name): self.name = name self.__items = [] # def add_item(self, item): self.__items.append(item) print("아이템 추가") # def get_number_of_item(self): return len(self.__items)
box = Box("Box") box.add_item("Item1") box.add_item("Item2") print(box.name) print(box.get_number_of_item()) #print(box.__items) #아이템 추가 #아이템 추가 #Box #2
class SuperClass(object): pass # # class SubClass(SuperClass): pass
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() #Method Overriding
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() #Speed: 240 #Speed: 180