일요일[짜빠구리 2인분은 버겁다]
공부는 혼자..
1. python 기초강의
2. opic 강의
3. 이것이
1>
만든 클래스 안에 함수를 만들어
class가 가진 메소드를
함수를 만들었다가 별도로 저장하지 않아도 됨
class Human():
'''인간'''
def create_human(name, weight):
person = Human()
person.name = name
person.weight = weight
return person
def eat(person):
person.weight += 3
print("{}은 {}kg이 되었습니다.냠냠".format(person.name, person.weight))
def run(person):
person.weight -= 1
print("{}은 {}kg이 되었습니다.헥헥".format(person.name, person.weight))
person = Human.create_human("하률",59)
person.eat()
person.eat()
person.run()
해당 person에 humanclass 매개변수를 정의했기때문에
person메소드에 인자값을 아무것도 넣지 않아도 알아서 해당 객체 가 가진 값으로 변환되어 적용된다.
class 메소드에 인자 2개를 넣는 것처럼 보여도 self 매개변수가 아닌 다른 인자만 기입하여도 정상동작함
특수한 메소드
def __init__(self):
해당 메소드는 인스턴스를 만들기만 하여도 기본으로 실행되는 메소드이다.
해당클래스에 인스턴스를 만들 때 init 함수의 매개변수에 따른 인자값을 넣어야 줘야 한다.
예시 )
class Human():
'''인간'''
def __init__(self,name, weight):
'''초기화 함수'''
print("__init__실행")
print("안녕하세요 제 이름은 {}이고 몸무게는 {}입니다.".format(name, weight))
self.name = name
self.weight = weight
def __str__(self):
'''문자열화 함수'''
def eat(person):
person.weight += 3
print("{}은 {}kg이 되었습니다.냠냠".format(person.name, person.weight))
def run(person):
person.weight -= 1
print("{}은 {}kg이 되었습니다.헥헥".format(person.name, person.weight))
person1 = Human("정원",32)
person = Human("하률",59)
person.eat()
person.eat()
person.run()
def __str__(self):
해당 클래스에 속한 메소드는 인스턴스를 만든 후 print하면 str 함수 안에 있는 return 값이 실행된다
예시 )
class Human():
'''인간'''
def __init__(self,name, weight):
'''초기화 함수'''
print("__init__실행")
print("안녕하세요 제 이름은 {}이고 몸무게는 {}입니다.".format(name, weight))
self.name = name
self.weight = weight
def __str__(self):
'''문자열화 함수'''
return "{}(몸무게 : {})".format(self.name, self.weight)
def eat(person):
person.weight += 3
print("{}은 {}kg이 되었습니다.냠냠".format(person.name, person.weight))
def run(person):
person.weight -= 1
print("{}은 {}kg이 되었습니다.헥헥".format(person.name, person.weight))
person1 = Human("정원",32)
person = Human("하률",59)
person.eat()
person.eat()
person.run()
print(person1)
class 상속
class 안에 또 다른 클래스를 매개변수로 받게되는 것을 상속이라고 하며
상속받은 클래스는 부모 클래스의 메소드를 자유롭게 사용이 가능하다
예시 )
class Animal() :
def eat(self):
print("먹는다")
def walk(self):
print("걷는다")
class Human(Animal):
def wave(self):
print("손을 흔든다")
class Dog(Animal):
def wag(self):
print("꼬리를 흔든다")
person = Human()
person.eat()
person.wave()
dog =Dog()
dog.eat()
dog.wag()
※ class안에 메소드의 매개변수로 self 가 들어가야 한다.
단순 오버라이딩(덮어쓴다)
재정의
class Animal() :
def eat(self):
print("먹는다")
def walk(self):
print("걷는다")
def greet(self):
print("인사한다")
class Human(Animal):
def wave(self):
print("손을 흔든다")
def greet(self):
self.wave()
class Dog(Animal):
def wag(self):
print("꼬리를 흔든다")
person = Human()
person.greet()
부모의 동작을 불러오는 super()
super().부모클래스의 메소드이름()
자식클래스에서 오버라이드한 메소드에서 다시 부모의 메소드를 불러올 때 사용한다
예시 )
class Animal() :
def eat(self):
print("먹는다")
def walk(self):
print("걷는다")
def greet(self):
print("인사한다")
class Human(Animal):
def wave(self):
print("손을 흔들면서", end=' ')
def greet(self):
self.wave()
super().greet()
person = Human()
person.greet()
super로 가져올 때 해당 매개변수로 self 인자값을 제외한 값은 기입해야한다.
class Animal() :
def __init__(self, name):
self.name = name
def eat(self):
print("먹는다")
def walk(self):
print("걷는다")
def greet(self):
print("{}가 인사한다".format(self.name))
class Human(Animal):
def __init__(self, name, hand):
super().__init__(name)
self.hand = hand
def wave(self):
print("{}손을 흔들면서".format(self.hand), end=' ')
def greet(self):
self.wave()
super().greet()
person = Human("하률이", "왼")
person.greet()
예시 )
class Animal() :
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print("먹는다")
def walk(self):
print("걷는다")
def greet(self):
print("{}가 인사한다".format(self.name))
class Human(Animal):
def __init__(self, name, age, hand):
super().__init__(name, age) #init 인자값 2개
self.hand = hand
def wave(self):
print("{}손을 흔들면서".format(self.hand), end=' ')
def introduce(self):
print("나이는 {}살 입니다.".format(self.age))
def greet(self):
self.wave()
super().greet()
person = Human("하률이",23,"왼")
person.greet()
person.introduce()
내 예외 만들기
from Create_method import MymakeExceptionError #Create_method file 의 MymakeExceptionError 클래스
a = input()
try :
if a not in ['가위','바위','보']:
raise MymakeExceptionError
except Exception as ex:
print("에러가 발생하였습니다.",ex)
List Comprehension
# 기존방식
alias = []
for i in range(1,11):
alias = alias + [i*i]
print(alias)
# Comprehension
alias = [ i*i for i in range(1,11)]
print(alias)
i를 가지고 계산하는 식은 가장 앞에
# if문 추가
alias = [i*i for i in range(1,11) if i % 2 == 0] #if 조건을 만족하는 i만 적용
활용 예시 )
# 2차원 배열 쉽게 만들기
[ (x,y) for x in range(15) for y in range(15)]
'''
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (0, 11), (0, 12), (0, 13), (0, 14), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (1, 13), (1, 14), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (2, 11), (2, 12), (2, 13), (2, 14), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (3, 11), (3, 12), (3, 13), (3, 14), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (4, 11), (4, 12), (4, 13), (4, 14), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (9, 11), (9, 12), (9, 13), (9, 14), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10), (10, 11), (10, 12), (10, 13), (10, 14), (11, 0), (11, 1), (11, 2), (11, 3), (11, 4), (11, 5), (11, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 11), (11, 12), (11, 13), (11, 14), (12, 0), (12, 1), (12, 2), (12, 3), (12, 4), (12, 5), (12, 6), (12, 7), (12, 8), (12, 9), (12, 10), (12, 11), (12, 12), (12, 13), (12, 14), (13, 0), (13, 1), (13, 2), (13, 3), (13, 4), (13, 5), (13, 6), (13, 7), (13, 8), (13, 9), (13, 10), (13, 11), (13, 12), (13, 13), (13, 14), (14, 0), (14, 1), (14, 2), (14, 3), (14, 4), (14, 5), (14, 6), (14, 7), (14, 8), (14, 9), (14, 10), (14, 11), (14, 12), (14, 13), (14, 14)]
'''
dict 예시 )
students = ['har','yuen', 'ryul']
students_dict = {number + 1 : name for number, name in enumerate(students)}
print(students_dict)
zip은 두개 이상의 인덱스나 스트링을 받아들여서 인덱스에 맞게 for in 문에서 하나씩 던저줄 수 있게 만들어주는 역할을 함
예시 )
score = [10,30,40,50,60]
stud = ['유리','철수','훈이','짱구','맹구']
for x, y in zip(score,stud):
print(x,y)
#10 유리
#30 철수
#40 훈이
#50 짱구
#60 맹구
dict Comprehension zip
예시 )
dict = {stud : score for stud, score in zip(stud, score)}
// yesterday wished to today list.
. https://school.programmers.co.kr/learn/courses/2/lessons/325 여기서부터 다시 시작_✔️
. https://www.hackers.co.kr/?c=s_lec/lec_speak/lec_Speaking_OPIc_movies&part=opic 강의 1시간 보기
. 이것이 진도 나가기
tomorrow wish list
. 이것이 진도 나가기
. 프로그래머스 알고리즘 하나 풀기
. opic 강의 보기_https://www.hackers.co.kr/?c=s_lec/lec_speak/lec_Speaking_OPIc_movies&part=opic
토익스피킹&오픽 해설강의 :: 해커스영어
토익스피킹&오픽 독학을 위한 무료인강! 해커스 토스&오픽 선생님의 노하우 전수!, 토스오픽 해설강의 무료보기, 오픽인강 무료보기
www.hackers.co.kr
. 도서관에서 딴짓 하지 않기
. python 강의 못들은 거 다 수강하기https://school.programmers.co.kr/learn/courses/2/lessons/340
'IT To do and To was' 카테고리의 다른 글
22년 11월 22일_왕실의 나이트, 프로그래머스 알고리즘, SQL (2) | 2022.11.22 |
---|---|
22년 11월 21일_앞으로 나아가면 온세상 사람들을 자꾸자꾸 보겠네 (0) | 2022.11.21 |
22년 11월 19일_python3 예외처리, list slice 객체 클래스 및 기초 개념 뇌새김, 오픽 예상문제 정리 (0) | 2022.11.19 |
22년 11월 18일_엉망인 날은 내 인생에 왜 넣어 SQL문제 및 큰수의 법칙 완벽정리 + 파이썬 놓치고 있는 기초 뇌새김 (2) | 2022.11.18 |
22년 11월 17일_알고리즘 문제 풀이 사이트 모음 및 커뮤니티 모음 + 금일 공부한 것 (0) | 2022.11.17 |