Post

[Python] OOP ๋”•์…”๋„ˆ๋ฆฌ datetime

๐Ÿ“Œ ๋“ค์–ด๊ฐ€๋ฉฐ

๋ณธ๊ฒฉ์ ์œผ๋กœ ๊ฐ์ฒด์ง€ํ–ฅ(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()

Desktop View

ํ‚ค์›Œ๋“œ์˜๋ฏธ
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) ... ํŠœํ”Œ๋กœ

Desktop View

๐Ÿ’ก zip์€ ๋ณต์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ์ฒ˜๋Ÿผ ์›์†Œ๋ผ๋ฆฌ ์—ฎ๋Š”๋‹ค. print(x, y)๋ฉด ๋‘˜์”ฉ, print(x)๋ฉด ํ•˜๋‚˜์˜ ํŠœํ”Œ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.


4. datetime

1
2
3
import datetime

print(datetime.datetime.now())   # ์—ฐ,์›”,์ผ,์‹œ,๋ถ„,์ดˆ,๋งˆ์ดํฌ๋กœ์ดˆ

Desktop View

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)                 # ํฌ๋ฆฌ์Šค๋งˆ์Šค๊นŒ์ง€ ๋‚จ์€ ์‹œ๊ฐ„

Desktop View


๐Ÿ“ ์ •๋ฆฌ

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 ๊ฐ™์€ ํŒŒ์ด์ฌ๋‹ค์šด ๊ฐ„๊ฒฐํ•œ ๋ฌธ๋ฒ•์„ ๊ณ๋“ค์ด๋ฉด ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ์šฐ์•„ํ•ด์ง„๋‹ค.

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.