μ½”λ“œ - 패캠 μˆ˜μ—… μ½”λ“œ μ°Έκ³  (패캠 μˆ˜μ—… 정리)

 

<이전 κΈ€>

https://silvercoding.tistory.com/20

 

[python 심화] 3. 클래슀 λ©”μ†Œλ“œ, μΈμŠ€ν„΄μŠ€ λ©”μ†Œλ“œ, μŠ€ν…Œμ΄ν‹± λ©”μ†Œλ“œ

μ½”λ“œ - 패캠 μˆ˜μ—… μ½”λ“œ μ°Έκ³  (패캠 μˆ˜μ—… 정리) <이전 κΈ€> https://silvercoding.tistory.com/19 https://silvercoding.tistory.com/18 [python 심화] 1. 객체지ν–₯(OOP), 클래슀 기초 μ‚¬μš© μ½”λ“œ - 패캠 μˆ˜μ—… μ½”λ“œ..

silvercoding.tistory.com

 

 

<데이터 λͺ¨λΈ μ°Έκ³  λ¬Έμ„œ> 

https://docs.python.org/3/reference/datamodel.html

 

3. Data model — Python 3.9.6 documentation

A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define

docs.python.org

 

[ 객체 (Objects)λŠ” 파이썬이 데이터(data)λ₯Ό μΆ”μƒν™”ν•œ 것(abstraction)μž…λ‹ˆλ‹€. 파이썬 ν”„λ‘œκ·Έλž¨μ˜ λͺ¨λ“  λ°μ΄ν„°λŠ” κ°μ²΄λ‚˜ 객체 κ°„μ˜ κ΄€κ³„λ‘œ ν‘œν˜„λ©λ‹ˆλ‹€.

 

λͺ¨λ“  κ°μ²΄λŠ” 아이덴티티(identity), ν˜•(type), κ°’(value)을 κ°–μŠ΅λ‹ˆλ‹€. 객체의 μ•„이덴티티 λŠ” ν•œ 번 λ§Œλ“€μ–΄μ§„ ν›„μ—λŠ” λ³€κ²½λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ©”λͺ¨λ¦¬μƒμ—μ„œμ˜ 객체의 μ£Όμ†Œλ‘œ 생각해도 μ’‹μŠ΅λ‹ˆλ‹€. γ€ˆis〉 μ—°μ‚°μžλŠ” 두 객체의 아이덴티티λ₯Ό λΉ„κ΅ν•©λ‹ˆλ‹€. id() ν•¨μˆ˜λŠ” 아이덴티티λ₯Ό μ •μˆ˜λ‘œ ν‘œν˜„ν•œ 값을 λŒλ €μ€λ‹ˆλ‹€. ]

 

 

μœ„μ˜ λ¬Έμ„œμ—μ„œ λ‚˜μ™€μžˆλŠ” 객체의 μ„€λͺ…이닀. λͺ¨λ“  κ°μ²΄λŠ” id, type, valueλ₯Ό κ°–λŠ”λ‹€. 예λ₯Ό λ“€μ–΄ 

a = 7 
print(id(a), type(a), dir(a))

μ΄λŸ¬ν•œ aλΌλŠ” λ³€μˆ˜λ„ κ°μ²΄λΌλŠ” 것이닀. λ”°λΌμ„œ id, type, valueλ₯Ό λͺ¨λ‘ κ°–κ³  μžˆλ‹€λŠ” 것을 μ•Œ 수 μžˆλ‹€. 

 

 

 

- λ„€μž„νŠΈ νŠœν”Œ 

<두 점 μ‚¬μ΄μ˜ 거리λ₯Ό κ΅¬ν•˜λŠ” 예제>

(1) 일반적인 νŠœν”Œ μ‚¬μš©

pt1 = (1.0, 5.0)
pt2 = (2.5, 1.5)
from math import sqrt
from typing import ClassVar
line_leng1 = sqrt((pt2[0] - pt1[0]) ** 2 + (pt2[1] - pt1[1]) ** 2)
print('EX1-1 : ', line_leng1)

μ΄λ ‡κ²Œ 인덱슀λ₯Ό μ΄μš―ν•˜μ—¬ κ΅¬ν•˜κ²Œ λœλ‹€. μ΄λ ‡κ²Œ κ°„λ‹¨ν•œ μ˜ˆμ œμ—μ„œλŠ” 상관이 μ—†κΈ΄ ν•˜μ§€λ§Œ 가독성이 λ–¨μ–΄μ§€λŠ” 것 κ°™κΈ΄ ν•˜λ‹€. 쑰금 더 λ³΅μž‘ν•œ μˆ˜μ‹μ΄λΌλ©΄ μ‹€μˆ˜ν•˜κΈ° λ”±μ’‹λ‹€! xκ°’ 끼리 λΉΌμ£Όκ³  , yκ°’ 끼리 λΉΌμ£ΌλŠ” 것을 더 λͺ…μ‹œμ μœΌλ‘œ 적어쀄 수 μžˆλŠ” named tuple을 μ‚¬μš©ν•΄ 보자! 

 

 

(2) λ„€μž„λ“œ νŠœν”Œ μ‚¬μš© 

*** named tuple 선언법 

from collections import namedtuple

Point1 = namedtuple('Point', ['x', 'y'])
Point2 = namedtuple('Point', 'x, y')
Point3 = namedtuple('Point', 'x y')
Point4 = namedtuple('Point', 'x y x class', rename=True) # Defalult=False

# 좜λ ₯ 
print('EX2-1 - ', Point1, Point2, Point3, Point4)

# Dict to Unpacking
temp_dict = {'x' : 75, 'y' : 55}

# 객체 생성 
print()
p1 = Point1(x=10, y=35)
p2 = Point2(20, 40)
p3 = Point3(45, y=20)
p4 = Point4(10, 20, 30, 40)
p5 = Point3(**temp_dict)



print('EX2-2 - ', p1, p2, p3, p4, p5)

collections의 namedtuple을 importν•΄μ€€λ‹€. λ„€μž„λ“œ νŠœν”Œμ€ 클래슀둜 ν•˜κΈ°μ—” λ„ˆλ¬΄ ν—€λΉ„ν•˜κ³ , νŠœν”Œμ— 이름을 μ£Όκ³  싢을 λ•Œ μ‚¬μš©ν•œλ‹€. 

 

point4μ—μ„œλŠ” xκ°€ μ€‘λ³΅μœΌλ‘œ λ“€μ–΄ κ°€ 있고, class도 λ³€μˆ˜λͺ…μœΌλ‘œ μ‚¬μš©ν•  수 μ—†μœΌλ―€λ‘œ rename=Trueλ₯Ό μ„€μ •ν•΄ 놓지 μ•ŠλŠ”λ‹€λ©΄ 였λ₯˜κ°€ λ‚  것이닀. 

rename=Trueλ₯Ό μ„€μ •ν•΄ λ†“μœΌλ©΄ μœ„μ™€κ°™μ΄ μ•Œμ•„μ„œ λ³€μˆ˜λͺ…이 λ°”λ€Œκ²Œ λœλ‹€. 

 

 

*** μ‚¬μš©

print('EX3-1 - ', p1[0] + p2[1])  # Index Error 주의 
print('EX3-2 - ', p1.x + p2.y) # 클래슀 λ³€μˆ˜ μ ‘κ·Ό 방식

# Unpacking 
x, y = p3

print('EX3-3 - ', x + y)

κΈ°μ‘΄ tupleκ³Ό 같이 Unpacking도 κ°€λŠ₯ν•˜λ‹€. 

 

 

*** λ„€μž„λ“œνŠœν”Œμ˜ λ©”μ†Œλ“œ 

  • _make() : μƒˆλ‘œμš΄ 객체λ₯Ό 생성 (리슀트 ---> λ„€μž„λ“œνŠœν”Œ) 
p4 = Point1._make(temp)
print('EX4-1 - ', p4)
  • _field : ν•„λ“œ  λ„€μž„ 확인 
print('EX4-2 - ', p1._fields, p2._fields, p3._fields)
  • _asdict() : OrderDict λ°˜ν™˜ 
# _asdict() : OrderDict λ°˜ν™˜ 
print('EX4-3 - ', p1._asdict(), p1._asdict(), p3._asdict(), p4._asdict())
  • _replace() : μˆ˜μ •λœ 'μƒˆλ‘œμš΄' 객체 λ°˜ν™˜ 
print('EX4-4 - ', p2._replace(y=100)) 

μ΄λ ‡κ²Œ ν•„λ“œμ— λŒ€ν•œ 값을 변경해쀄 수 μžˆλŠ”λ°, μ΄λŠ” 리슀트 처럼 값이 λ³€κ²½λ˜λŠ” 것이 μ•„λ‹ˆλΌ , μ•„μ˜ˆ μƒˆλ‘œμš΄ 객체λ₯Ό λ°˜ν™˜ν•΄ μ£ΌλŠ” 것이닀. 

 

 

 

 

*** 예제

# λ„€μž„λ“œ νŠœν”Œ μ„ μ–Έ 
Point = namedtuple('Point', 'x y')

# 두 점 μ„ μ–Έ 
pt1 = Point(1.0, 5.0)
pt2 = Point(2.5, 1.5)

# 두 점 μ‚¬μ΄μ˜ 거리 계산
line_leng2 = sqrt((pt2.x - pt1.x) ** 2 + (pt2.y - pt1.y) ** 2)

# 좜λ ₯
print("EX1-2 - ", line_leng2)
print("EX1-3 - ", line_leng1 == line_leng2)

 

κ·Έλž˜μ„œ μ›λž˜ ν•˜λ €ν–ˆλ˜ 예제λ₯Ό λ„€μž„λ“œ νŠœν”Œμ„ μ‚¬μš©ν•˜λ©΄ μœ„μ™€ κ°™λ‹€. λ„€μž„λ“œνŠœν”Œμ„ μ‚¬μš©ν•˜λ©΄ μœ„μ™€κ°™μ΄ 더 λͺ…μ‹œμ μœΌλ‘œ μˆ˜μ‹μ„ μž‘μ„±ν•΄μ€„ 수 μžˆλ‹€. 

 

 

 

- λ„€μž„λ“œ νŠœν”Œ μ‹€μŠ΅ 

학생 그룹을 μƒμ„±ν•˜λŠ” 예제, 4개의 반(A, B, C, D) μ—μ„œ 각각 20λͺ…μ˜ 학생이 쑴재

 

(1) λ„€μž„λ“œ νŠœν”Œ μ„ μ–Έ 

Classes = namedtuple('Classed', ['rank', 'number'])

 

 

(2) κ·Έλ£Ή 리슀트 μ„ μ–Έ (List Comprehension μ‚¬μš©)

# List Comprehension
numbers = [str(n) for n in range(1, 21)]
ranks = 'A B C D'.split()
print(ranks, numbers)

print()

# List Comprehension
students = [Classes(rank, number) for rank in ranks for number in numbers]

print('EX5-1 - ', len(students))
print('EX5-2 - ', students[4].rank)

numberλŠ” 1~20의 문자λ₯Ό λ‹΄κ³ μžˆλŠ” 리슀트, ranksλŠ” A~B의 문자λ₯Ό λ‹΄κ³  μžˆλŠ” λ¦¬μŠ€νŠΈμ΄λ‹€. 

이제 λ„€μž„λ“œ νŠœν”Œμ˜ 객체듀을 list comprehension을 μ‚¬μš©ν•˜μ—¬ 생성해 μ€€λ‹€. 총 20 * 4 = 80 개의 객체가 리슀트 μ•ˆμ— 생성될 것이닀. 

 

 

*** 가독성이 λ–¨μ–΄μ§€μ§€λ§Œ ν•œλ²ˆμ— 그룹리슀트λ₯Ό λ§Œλ“œλŠ”λ²• 

students2 = [Classes(rank, number) for rank in 'A B C D'.split() for number in [str(n) for n in range(1, 21)]]

print('EX6-1 - ', len(students2))
print('EX6-1 - ', students2)

λ¦¬μŠ€νŠΈμ— ν•œλ²ˆμ— λ•Œλ €λ°•μ•„ μ£ΌλŠ” 법이닀. 뭐든지 κ³Όν•˜λ©΄ 독이 λ˜λŠ” 것 κ°™λ‹€. 가독성이 λ–¨μ–΄μ§€μ§€λ§Œ 이런 방법도 μžˆλ‹€λŠ” 것을 μ•Œμ•„λ‘κΈ°. 

 

 

+ Recent posts