alt + shift + 방향키 = 그 줄 옮겨짐
class Monster:
def __init__(self, name, health, attack):
self.name = name
self.health = health
self.attack = attack
def move(self):
print(f"[{self.name}] 지상에서 이동하기")
class Dragon(Monster):
# 생성자 오버라이딩
def __init__(self, name, health, attack, skill):
super().__init__(name, health, attack)
self.skill = skill
super
: 부모 클래스 의미
생성자를 그대로 가져올 수 있음
Pycharm에서 내가 만든 모듈이 import 안될 때: Settings - Project Structure에서 실행할 디렉토리를 Sources로 파랗게 만든다
__name__
: 지금 파일 이름
__name__ == "__main__"
: 해당 파일을 직접 실행했다
unit/__init__.py에 from . import 모듈명들
을 입력해 두면 다른 모듈에서 from unit import *
, impot unit
을 사용할 수 있다.
import pickle
# 쓰기
file = open("data.pickle", "wb")
pickle.dump(data, file)
# 읽기
file = open("data.pickle", "rb")
data = pickle.load(file)
.pickle
== .p
== .pic
wb
, rb
: write binary, read binary
try:
예외 발생할 수 있는 코드
except:
예외 발생 시 실행할 코드
else:
예외 발생하지 않은 경우 실행할 코드
finally:
항상 실행할 코드
에러 종류 출력
except ZeroDivisionError as e:
print(e)
>> division by zero
raise
에러 강제 발생
raise Exception
raise Exception("에러 메세지")
예외 계층 구조
except ArithmeticError:
시 하위의 세 예외 모두 처리 가능하다
에러 만들기
class 새 예외(Exception):
def __init__(self):
super().__init__("에러 메세지")
try:
if 새 예외 상황:
raise 새 예외
create, read, update, delete