๐ ๋ค์ด๊ฐ๋ฉฐ
๋ณธ๊ฒฉ์ ์ผ๋ก ๊ฐ์ฒด์งํฅ(OOP)์ ๋ค์ด๊ฐ๋ค. ์์ยท์ค๋ฒ๋ผ์ด๋ฉ, ๊ทธ๋ฆฌ๊ณ ๋ฆฌ์คํธ ๋ดํฌยทzipยทdatetime ๊ฐ์ ์ค์ฉ ๋ฌธ๋ฒ๊น์ง ์ ๋ฆฌํ๋ค.
1. ์์ธ๋ก ๋ฐ๋ณต๋ฌธ ํ์ถํ๊ธฐ
1
2
3
4
5
6
7
8
9
10
| classrooms = {'1๋ฐ': [162,175,198,137,145,199], '2๋ฐ': [165,177,157,160,199]}
try:
for n in classrooms.keys():
for a in classrooms[n]: # ์ด์ค for๋ก ์์๊น์ง ์ ๊ทผ
if a > 190:
print('{}์ 190๋๋ ์ฌ๋์ด ์์ต๋๋ค.'.format(n))
raise StopIteration # ์์ธ๋ก ๋ฐ๋ณต ํ์ถ
except StopIteration:
print('190์ด ๋๋ ํ์์ ์ฐพ์์ต๋๋ค.')
|
๐ก ๋์
๋๋ฆฌ๋ก ์ ๊ทผํด๋ ์์๊น์ง ๋ฟ์ผ๋ ค๋ฉด ์ด์ค for๊ฐ ํ์ํ๋ค. ์ค์ฒฉ ๋ฐ๋ณต์ ๋น ์ ธ๋์ฌ ๋ break๋ฅผ ๋ ๋ฒ ์ฐ๋ ๋์ , raise StopIteration์ผ๋ก ํ ๋ฒ์ except๋ก ๋์ด๊ฐ๋ฉด ์ ์ฉํ๋ค.
2. ์์๊ณผ ์ค๋ฒ๋ผ์ด๋ฉ
๋ถ๋ชจ Animal์ Human, Cat์ด ์์ํ๋ค.
1
2
3
4
5
6
| class Animal():
def __init__(self, name):
self.name = name
def walk(self): print('๊ฑท๋๋ค')
def eat(self): print('๋จน๋๋ค')
def greet(self): print('{}์ด/๊ฐ ์ธ์ฌํ๋ค'.format(self.name))
|
1
2
3
4
5
6
7
8
9
10
11
| class Human(Animal):
def __init__(self, name, hand):
super().__init__(name) # ๋ถ๋ชจ ์์ฑ์ ํธ์ถ (name ์ ๋ฌ)
self.hand = hand # ์์์ hand๋ง ์ฒ๋ฆฌ
def wave(self): print('{}์ ํ๋ ๋ค'.format(self.hand))
def greet(self): # ์ค๋ฒ๋ผ์ด๋ฉ (์์์ด ์ฐ์ )
self.wave() # self = ์๊ธฐ ์์
super().greet() # super() = ๋ถ๋ชจ
person1 = Human('๋ํฌ', '์ค๋ฅธ์')
person1.greet()
|
| ํค์๋ | ์๋ฏธ |
|---|
super().__init__(name) | ๋ถ๋ชจ ์์ฑ์ ํธ์ถ |
self | ์๊ธฐ ์์ |
super() | ๋ถ๋ชจ |
๐ก ๊ฐ์ ์ด๋ฆ์ ๋ฉ์๋๋ฅผ ์ฌ์ ์ํ๋ฉด ์์์ด ์ฐ์ ๋๋ค. Human.greet()์ ์์ ์ wave()์ ๋ถ๋ชจ์ greet()๋ฅผ ํจ๊ป ํธ์ถํ๋ค.
1
2
| class Cat(Animal):
def walk(self): print("์ฐ์ํ๊ฒ ๊ฑท๋๋ค") # walk๋ง ์ฌ์ ์
|
3. ๋ฆฌ์คํธ ๋ดํฌ ยท enumerate ยท zip
๋ฆฌ์คํธ ๋ดํฌ(comprehension) โ ํ ์ค๋ก ๋ฆฌ์คํธ ์์ฑ:
1
| print([n*n for n in range(1, 11) if n % 2 == 0]) # ์ง์์ ์ ๊ณฑ
|
enumerate โ ์ธ๋ฑ์ค์ ๊ฐ์ ํจ๊ป:
1
2
3
4
5
6
| students = ['๊น๊ฐ๋ฅ', '๊น๋ง๋ฅ', '๊น์๋ฅ']
for num, name in enumerate(students):
print('{}๋ฒ {} ํ์'.format(num + 1, name))
# ๋์
๋๋ฆฌ ๋ดํฌ๋ก๋
students_dict = {"{}๋ฒ".format(n+1): name for n, name in enumerate(students)}
|
zip โ ์ฌ๋ฌ ๋ฆฌ์คํธ๋ฅผ ์ง์ง์ด ๋ฌถ๊ธฐ:
1
2
3
4
5
6
7
| students = ['๊น๊ฐ๋ฅ', '๊น๋ง๋ฅ', '๊น์๋ฅ']
scores = [90, 98, 97]
for x, y in zip(students, scores):
print(x, y) # ๊น๊ฐ๋ฅ 90 ...
for x in zip(students, scores):
print(x) # ('๊น๊ฐ๋ฅ', 90) ... ํํ๋ก
|
๐ก zip์ ๋ณต์์ ๋ฆฌ์คํธ๋ฅผ ๋์
๋๋ฆฌ์ฒ๋ผ ์์๋ผ๋ฆฌ ์ฎ๋๋ค. print(x, y)๋ฉด ๋์ฉ, print(x)๋ฉด ํ๋์ ํํ๋ก ์ถ๋ ฅ๋๋ค.
4. datetime
1
2
3
| import datetime
print(datetime.datetime.now()) # ์ฐ,์,์ผ,์,๋ถ,์ด,๋ง์ดํฌ๋ก์ด
|
1
2
3
4
5
6
| stime = datetime.datetime.now()
stime.replace(hour=2) # ํน์ ๋ถ๋ถ๋ง ๋ณ๊ฒฝ
# ๋ ์ง ๋บ์
โ ๋จ์ ์๊ฐ
christmas = datetime.datetime(2021,12,25,11,30,30) - datetime.datetime.now()
print(christmas) # ํฌ๋ฆฌ์ค๋ง์ค๊น์ง ๋จ์ ์๊ฐ
|
๐ ์ ๋ฆฌ
1
2
3
4
5
6
| OOP ยท ์ค์ฉ ๋ฌธ๋ฒ
โโ ์์ class ์์(๋ถ๋ชจ): + super() + ์ค๋ฒ๋ผ์ด๋ฉ(์์ ์ฐ์ )
โโ ๋ฆฌ์คํธ ๋ดํฌ [์ for x in ... if ์กฐ๊ฑด]
โโ enumerate ์ธ๋ฑ์ค+๊ฐ ๋์ ์ํ
โโ zip ์ฌ๋ฌ ๋ฆฌ์คํธ๋ฅผ ์ง์ง์ด ๋ฌถ๊ธฐ
โโ datetime now(), replace(), ๋ ์ง ๋บ์
์ผ๋ก ๊ธฐ๊ฐ ๊ณ์ฐ
|
| ๊ฐ๋
| ํ ์ค ์ ์ |
|---|
| super() | ๋ถ๋ชจ ํด๋์ค ์ฐธ์กฐ |
| ๋ฆฌ์คํธ ๋ดํฌ | ํ ์ค๋ก ๋ฆฌ์คํธ ์์ฑ |
| zip | ์ฌ๋ฌ ๋ฆฌ์คํธ๋ฅผ ์์๋ผ๋ฆฌ ๋ฌถ๊ธฐ |
| datetime | ๋ ์งยท์๊ฐ ์ฒ๋ฆฌ |
ํ์ด์ฌ OOP๋ super()์ ์ค๋ฒ๋ผ์ด๋ฉ(์์ ์ฐ์ )๋ง ์ก์ผ๋ฉด ์ฝ๋ค. ์ฌ๊ธฐ์ ๋ฆฌ์คํธ ๋ดํฌยทzipยทenumerate ๊ฐ์ ํ์ด์ฌ๋ค์ด ๊ฐ๊ฒฐํ ๋ฌธ๋ฒ์ ๊ณ๋ค์ด๋ฉด ์ฝ๋๊ฐ ํจ์ฌ ์ฐ์ํด์ง๋ค.
Comments powered by Disqus.