๊ด€๋ฆฌ ๋ฉ”๋‰ด

Done is Better Than Perfect

[Fluent Python ์Šคํ„ฐ๋””] Study Note #1 Data Model ๋ณธ๋ฌธ

๐Ÿค– AI/๊ฐœ๋ฐœ๊ณต๋ถ€

[Fluent Python ์Šคํ„ฐ๋””] Study Note #1 Data Model

jimingee 2021. 11. 15. 20:23

 

โ˜ข๏ธ ํŒŒ์ด์ฌ์œผ๋กœ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณต๋ถ€๋ฅผ ํ•  ๋งŒํผ ํŒŒ์ด์ฌ์— ์ต์ˆ™ํ•˜์ง€๋งŒ ํŒŒ์ด์ฌ์˜ ์„ธ๊ณ„๋Š” ๊ณต๋ถ€ํ• ์ˆ˜๋ก ๋”์šฑ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™๋‹ค. 

 

โ˜ข๏ธํŒŒ์ด์ฌ์œผ๋กœ ํ•™์ˆ  ๊ณต๋ถ€๋ฅผ ํ•˜๋ ค๋ฉด ์ œ๋Œ€๋กœ ์•Œ์•„์•ผ ํ•œ๋‹ค๋Š” ๊ต์ˆ˜๋‹˜์˜ ์ถ”์ฒœ๊ณผ ํŒŒ์ด์ฌ์Šค๋Ÿฌ์›€(pythonic)์„ ๊นŠ์ด์žˆ๊ฒŒ ์ดํ•ดํ•˜๊ณ ์ž

'Fluent Python' ์Šคํ„ฐ๋””๋ฅผ ์‹œ์ž‘ํ•˜์˜€๋‹ค. 

 


1-1. Python Data Model, Special Method

โŒAbstract

ํŒŒ์ด์ฌ์ด ์ฒ˜์Œ ๋ฐฐ์šธ ๋•Œ ์ข‹์€ ์–ธ์–ด๋ผ๊ณ  ์ž์ฃผ ์–ธ๊ธ‰๋˜๋Š” ์ด์œ  ์ค‘ ํ•˜๋‚˜๋Š” ์–ธ์–ด์˜ ์ผ๊ด€์„ฑ ๋•Œ๋ฌธ์ด๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด, object์˜ ๊ฒฝ์šฐ len( ) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํฌ๊ธฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

์ด์™€ ์ผ๊ด€๋˜๊ฒŒ, ์‚ฌ์šฉ์ž ์ •์˜ ํด๋ž˜์Šค๋Š” __len__( )์™€ ๊ฐ™์€ ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 


๐Ÿ’ก Python magic method

ํŠน๋ณ„ ๋ฉ”์„œ๋“œ = ๋งค์ง ๋ฉ”์„œ๋“œ(magic method) = dunder method (์—ฌ๊ธฐ์„œ dunder๋Š” double under์˜ ์ค„์ž„๋ง)

 

๐Ÿ‘‰์•ž ๋’ค์— ์ด์ค‘ ์–ธ๋”๋ฐ” ์žˆ์Œ [์˜ˆ, __getitem__( )

๐Ÿ‘‰๊ธฐ๋ณธ์ ์ธ ์–ธ์–ด ๊ตฌ์กฐ์ฒด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Œ

๐Ÿ‘‰ํŠน๋ณ„ ๋ฉ”์„œ๋“œ๋Š” ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ

ํŒŒ์ด์ฌ ์†Œ์Šค ์ฝ”๋“œ์—์„œ๋Š” object.__len__()์œผ๋กœ ์ง์ ‘ ํ˜ธ์ถœ X  โžก๏ธ len(object) ํ˜•ํƒœ๋กœ ํ˜ธ์ถœ

๐Ÿ‘‰์ข…์ข… len( ), iter( ), str( ) ๊ณผ ๊ฐ™์€ ํŒŒ์ด์ฌ ๋‚ด์žฅํ•จ์ˆ˜๊ฐ€ ํ•ด๋‹น ํŠน๋ณ„ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ

[ํŒŒ์ด์ฌ ์ฝ”๋“œ] for i in x  โžก๏ธ  iter(x) ํ˜ธ์ถœ โžก๏ธ [ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ] x.__iter__( ) ํ˜ธ์ถœ

 

import collections

Student = collections.namedtuple('Student',['age', 'class_name'])

class Stud:
    age = [str(n) for n in range(10,15)]
    class_name = 'hana dol set net'.split()

    def __init__(self):
        self._std = [Student(age, class_name) for age in self.age 
        				for class_name in self.class_name]

    def __len__(self):
        return len(self._std)

    def __getitem__(self, position):
        return self._std[position]
        
 
friend = Stud()
print(len(friend))  #__len__( ) ํ•จ์ˆ˜๋ฅผ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ์ „๋‹ฌ
## 20

print(friend[0]) #__getitem__( ) ํ•จ์ˆ˜๋ฅผ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ์ „๋‹ฌ
## Student(age='10', class_name='hana')

print(friend[-1])
## Student(age='14', class_name='net')

print(friend[:3]) # list slicing ๊ฐ€๋Šฅ
## [Student(age='10', class_name='hana'), Student(age='10', class_name='dol'), Student(age='10', class_name='set')]

Stud ์˜ ํด๋ž˜์Šค์— __len__( ), __getitems__( )์˜ 2๊ฐœ์˜ ์‚ฌ์šฉ์ž ์ •์˜ ํŠน๋ณ„ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

 

๋™์ž‘ ๊ตฌ์กฐ ํ•ด์„

โžก๏ธ len(friend) ํ•จ์ˆ˜๋Š” __len__( ) ํ•จ์ˆ˜๋ฅผ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ์ „๋‹ฌํ•˜์˜€๋‹ค. 

โžก๏ธ friend[0]์€ __getitem__( ) ํ•จ์ˆ˜๋ฅผ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ์ „๋‹ฌํ•˜์˜€๋‹ค. 

โžก๏ธ friend[:3]์™€ ๊ฐ™์ด list slicing ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 


 

ํŠน๋ณ„ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„์˜ ์˜๋ฏธ
์‚ฌ์šฉ์ž ์ •์˜ ๊ฐ์ฒด๋„ ๋‚ด์žฅํ˜• ๊ฐ์ฒด์ฒ˜๋Ÿผ ์ž‘๋™  โžก๏ธ ํŒŒ์ด์ฌ์Šค๋Ÿฌ์šด(pythonic) ํ‘œํ˜„๋ ฅ์žˆ๋Š” ์ฝ”๋”ฉ ์Šคํƒ€์ผ ๊ตฌ์‚ฌ ๊ฐ€๋Šฅ

 


 

๐Ÿ’ก Vector class

๋‹ค์Œ์€ vector ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉ์ž ์ •์˜ ํŠน๋ณ„ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“  ๋ฐฉ์‹์ด๋‹ค. 

 

from math import hypot

class Complicated:

    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __repr__(self):
        return 'Complicated num(%r + %ri)' % (self.x, self.y)

    def __abs__(self): # sqrt(x*x + y*y)
        return hypot(self.x, self.y)

    def __bool__(self): # complicated์˜ ํฌ๊ธฐ๊ฐ€ 0์ด๋ฉด FALSE ๋ฐ˜ํ™˜, ์•„๋‹ˆ๋ฉด TRUE ๋ฐ˜ํ™˜
        return bool(abs(self))

    def __add__(self, other):
        x = self.x + other.x
        y = self.y + other.y
        return Complicated(x, y)

    def __mul__(self, scalar):
        return Complicated(self.x * scalar, self.y * scalar)

c1 = Complicated(2,4) # __repr__() ํ˜ธ์ถœ
c2 = Complicated(2,1)
print(c1+c2) # __add__( ) ํ˜ธ์ถœ
## Complicated num(4 + 5i)

c = Complicated(3,4)
print(abs(c)) # __abs__( ) ํ˜ธ์ถœ
## 5.0

print(c*3) # __abs__( ) ํ˜ธ์ถœ
## Complicated num(9 + 12i)

print(abs(c*3)) #__mul__( ) ํ˜ธ์ถœ
## 15.0

 

๐Ÿ‘‰ __repr__( )๊ณผ __str__( ) ๋น„๊ต

 __str__( ) : print() ํ•จ์ˆ˜ ํฌํ•จ๋จ , ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ฃผ๊ธฐ ์ ๋‹นํ•œ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜

__repr__( ) : ๋ฐ˜ํ™˜ํ•œ ๋ฌธ์ž์—ด ๋ช…ํ™•

โžก๏ธ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค๋ฉด __repr__( )์„ ๊ตฌํ˜•ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋” ๋งŽ๋‹ค

 

Comments