05.클래스2
클래스 2
3.클래스의 매직 메서드(magic method)
- 스페셜 메서드(special method)
- ‘_‘를 2개 붙여서 사용한다
- 특정상황에서 도작하는메서드
n = 0
print(type(n))
#dir(object): 해당 object(객체)가 가지고 있는 변수와 메소드를 리턴 해주는 함수
print(dir(int))
print(n + 20)
print(n.__add__(20)) #매직 메소드
<class 'int'>
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
20
20
strings = "hello world"
print(strings + '~hi')
print(strings.__add__('~hi'))
hello world~hi
hello world~hi
class Book:
# 생성자(초기화 함수)
def __init__(self, author, title, publisher, year):
print('__init__함수 호출')
self.author = author
self.title = title
self.publisher = publisher
self.year = year
def __str__(self):
return "Author : " + self.author + \
"Title : " + self.title + \
"\nPublisher : " + self.publisher + \
"\nYear : " + self.year
def print_info(self):
print('Author : ', self.author)
print('Title : ', self.title)
print('Publisher : ', self.publisher)
print('Year : ', self.year)
생성자 (초기화함수)-객체가 생성될떄 자동으로 호출하는 메서드
__init__를 사용하면 메소드는 생성자가 됨
_str__() 메서드는 객체를 인쇄 가능한 형식으로 인쇄하려고 할때 실행
book1 = Book('jh', 'python', 'jh출판사', '2020')
book1.print_info()
print(book1)
__init__함수 호출
Author : jh
Title : python
Publisher : jh출판사
Year : 2020
Author : jhTitle : python
Publisher : jh출판사
Year : 2020
class Yeolmu:
def __init__(self, name, age, color):
self.name = name
self.age = age
self.color = color
def print_info(self):
print(self.name)
print(self.age)
print(self.color)
petcard = Yeolmu('열무','7살','brown')
petcard.print_info()
열무
7살
brown
class House:
def __init__(self,rooms, addr):
self.rooms = rooms
self.addr = addr
def print_info(self):
print(f'집은 {self.addr}에 위치합니다...')
Myhouse = House(2, '홍도동')
Myhouse.print_info()
집은 홍도동에 위치합니다...
class Player:
# 생성자 : 객체초기화
def __init__(self, name, addr, tel):
print("객체 생성")
self.name = name
self.addr = addr
self.tel = tel
def print_info(self):
print(self.name)
print(self.addr)
print(self.tel)
player1 = Player('홍길동', '서울', '1234-1234')
player1.print_info()
print()
player2 = Player('손흥민', '런던', '1234-1234')
player2.print_info()
객체 생성
홍길동
서울
1234-1234
객체 생성
손흥민
런던
1234-1234
class House:
def __init__(self, rooms, addr):
self.rooms = rooms
self.addr = addr
def existAt(self):
print(f'집은 {self.addr}에 위치합니다...')
house1 = House(3, '100번지')
house1.existAt()
집은 100번지에 위치합니다...
댓글남기기