Search
๐Ÿ“˜

Introducing Python - Bill Lubanovic

1. Data: Type, Value, Variable, Name

๊ฐ€. ํŒŒ์ด์ฌ ๋ฐ์ดํ„ฐ์˜ ์ •์ฒด

1) Data is Object
โ€ข
๊ธฐ๋ณธ ๋‚ด์žฅ ๋ฐ์ดํ„ฐ๋Š” Object๋กœ Wrapping๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋จ
โ†’ ๊ธฐ๋ณธ ๋‚ด์žฅ ๋ฐ์ดํ„ฐ๋ž€ ์ •์ˆ˜, ๋ฌธ์ž์—ด, ํ•จ์ˆ˜ ๋“ฑ์ด ํ•ด๋‹น๋จ
โ†’ Object๋ž€ ์†์„ฑ(attribute)๊ณผ ๋ฉ”์†Œ๋“œ(method)๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ์ปค์Šคํ…€ ์ž๋ฃŒ๊ตฌ์กฐ
โ€ข
๊ธฐ๋ณธ ๋‚ด์žฅ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ธฐ๋Š” Object๋Š” ๋‹ค์Œ์˜ ๋„ค ๊ฐ€์ง€ ์†์„ฑ์„ ํฌํ•จํ•จ
โ†’ ํƒ€์ž…: ๋ฐ์ดํ„ฐ์˜ ์‚ฌ์šฉ๋ฐ˜๊ฒฝ ์ œํ•œ
โ†’ ID: ๊ฐ์ฒด์˜ ์œ ์ผ์„ฑ ํ‘œํ˜„
โ†’ ๊ฐ’: ๋‚ด์šฉ๋ฌผ
โ†’ ์ฐธ์กฐํšŸ์ˆ˜: ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ํŠน์ • ๊ฐ์ฒด๊ฐ€ ์ง€์ •๋œ ํšŸ์ˆ˜
2) Type
โ€ข
๊ฐ€๋ณ€์„ฑ(Mutable)
โ†’ ๊ฐ€๋ณ€์„ฑ์ด ์žˆ๋Š” ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋Š” ๊ฐ์ฒด ๋‚ด ๊ฐ’์˜ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•จ
โ†’ ํŒŒ์ด์ฌ ๋‚ด์žฅ ๋ฐ์ดํ„ฐ ์ค‘ ๊ฐ€๋ณ€์„ฑ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋Š” ๋ฆฌ์ŠคํŠธ, ๋”•์…”๋„ˆ๋ฆฌ, ๋ฐ”์ดํŠธ ๋ฐฐ์—ด, ์…‹์œผ๋กœ ํ•œ์ •๋จ
โ€ข
Strong Type Language
โ†’ ๊ฐ€๋ณ€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๊ฒฝ์šฐ์—๋„ ํƒ€์ž… ๋ณ€๊ฒฝ์€ ๋ถˆ๊ฐ€๋Šฅ
3) Name
โ€ข
๋ณ€์ˆ˜์— ๋ฐ์ดํ„ฐ๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ == ๋ณ€์ˆ˜์— ์ด๋ฆ„์„ ๋ถ™์ด๋Š” ๊ฒƒ
โ†’ ๋ณ€์ˆ˜์— ๋ฐ์ดํ„ฐ๋ฅผ ํ• ๋‹นํ•จ์œผ๋กœ์จ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์ด๋ฆ„์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ
โ†’ ๋ณ€์ˆ˜ ํ• ๋‹น์€ ๋ฐ์ดํ„ฐ ๋‚ด ๊ฐ’์„ ๋ณ€์ˆ˜์— ๋ณต์‚ฌํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜์ง€ ์•Š๋Š”๋‹ค
โ†’ ๋ณ€์ˆ˜์— ๋ฐ์ดํ„ฐ๋ฅผ ํ• ๋‹นํ•˜๋ฉด ๋ฐ์ดํ„ฐ ๋‚ด ์ฐธ์กฐํšŸ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•œ๋‹ค
โ€ข
(์˜ˆ์‹œ) ๋ณ€์ˆ˜ ํ• ๋‹น ํ”„๋กœ์„ธ์Šค
โ†’ ์ต๋ช… ์ •์ˆ˜ ๊ฐ์ฒด ์ƒ์„ฑ(๊ฐ’์€ 5)
โ†’ ๋ณ€์ˆ˜ y๊ฐ€ ์ต๋ช… ์ •์ˆ˜ ๊ฐ์ฒด ์ฐธ์กฐ
โ†’ ์ •์ˆ˜ ๊ฐ์ฒด ๋‚ด ์ฐธ์กฐํšŸ์ˆ˜ ์ฆ๊ฐ€
โ†’ ์ต๋ช… ์ •์ˆ˜ ๊ฐ์ฒด ์ƒ์„ฑ(๊ฐ’์€ 12)
โ†’ ๊ฐ’์ด 12์ธ ์ต๋ช… ์ •์ˆ˜ ๊ฐ์ฒด์˜ ๊ฐ’๊ณผ y๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ •์ˆ˜ ๊ฐ์ฒด์˜ ๊ฐ’์„ ๋บŒ
โ†’ ๊ฐ’ 7์€ ์ต๋ช… ์ •์ˆ˜ ๊ฐ์ฒด์— ํ• ๋‹น๋จ
โ†’ ๋ณ€์ˆ˜ x๊ฐ€ ์ต๋ช… ์ •์ˆ˜ ๊ฐ์ฒด ์ฐธ์กฐ(๊ฐ’ 7)
โ†’ ๊ฐ’์ด 7์ธ ๊ฐ์ฒด์˜ ์ฐธ์กฐํšŸ์ˆ˜ ์ฆ๊ฐ€
โ†’ x๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ •์ˆ˜ ๊ฐ์ฒด์˜ ๊ฐ’ ์ถœ๋ ฅ
# introducing python 2.7 chapter ์˜ˆ์ œ >>> y = 5 >>> x = 12 - y >>> x 7
Python
๋ณต์‚ฌ
4) ๊ฐ€๋ณ€ ๋ฐ์ดํ„ฐ์™€ ์ด๋ฆ„ ์žฌํ• ๋‹น
โ€ข
๋ถˆ๋ณ€ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ์— ๊ฐ’ ๋ณ€๊ฒฝ์€ ์ด๋ฆ„์„ ์žฌํ• ๋‹นํ•˜๋Š” ๊ฒƒ
โ†’ ๋ถˆ๋ณ€ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ์— ์ด๋ฆ„์ด ํ• ๋‹น๋œ ๊ฒฝ์šฐ, ์ด๋ฆ„์— ๋‹ค๋ฅธ ๊ฐ’์„ ์žฌํ• ๋‹นํ•˜๋Š” ๊ฒƒ์€ ์ด์ „ ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹˜
โ€ข
(์˜ˆ์‹œ) ์ด๋ฆ„ ์žฌํ• ๋‹น
โ†’ ๊ฐ’์ด 5์ธ ์ •์ˆ˜ ๊ฐ์ฒด ์ƒ์„ฑ
โ†’ ๋ณ€์ˆ˜ x๊ฐ€ ๊ฐ’์ด 5์ธ ์ •์ˆ˜ ๊ฐ์ฒด ์ฐธ์กฐ
โ†’ ๊ฐ’์ด 5์ธ ์ •์ˆ˜ ๊ฐ์ฒด์˜ ์ฐธ์กฐํšŸ์ˆ˜ ์ฆ๊ฐ€
โ†’ ๊ฐ’์ด 6์ธ ์ •์ˆ˜ ๊ฐ์ฒด ์ƒ์„ฑ
โ†’ ๋ณ€์ˆ˜ x๊ฐ€ ๊ฐ’์ด 6์ธ ์ •์ˆ˜ ๊ฐ์ฒด ์ฐธ์กฐ
โ†’ ๊ฐ’์ด 5์ธ ์ •์ˆ˜ ๊ฐ์ฒด์˜ ์ฐธ์กฐํšŸ์ˆ˜ ๊ฐ์†Œ
โ†’ ๊ฐ’์ด 6์ธ ์ •์ˆ˜ ๊ฐ์ฒด์˜ ์ฐธ์กฐํšŸ์ˆ˜ ์ฆ๊ฐ€
>>> x = 5 >>> x = 6
Python
๋ณต์‚ฌ
โ€ข
๊ฐ€๋ณ€ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ์— ๋ณต์ˆ˜์˜ ์ด๋ฆ„์„ ์ง€์ •ํ–ˆ๋‹ค๋ฉด ํ•˜๋‚˜์˜ ์ด๋ฆ„์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ์‹œ, ๋‹ค๋ฅธ ์ด๋ฆ„ ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝ๋œ ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
โ€ข
(์˜ˆ์‹œ) ๊ฐ€๋ณ€ ๋ฐ์ดํ„ฐ ์ด๋ฆ„ ์žฌํ• ๋‹น
x = [1, 2, 3] y = x y[0] = 4 print(x) # [4, 2, 3] print(y) # [4, 2, 3]
Python
๋ณต์‚ฌ

๋‚˜. ๊ธฐ๋ณธ์—ฐ์‚ฐ

1) bool ๊ฐ’๊ณผ ๋…ผ๋ฆฌ์—ฐ์‚ฐ
โ€ข
์ˆซ์ž 0์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์ˆ˜ - true
โ€ข
๋นˆ ๋”•์…”๋„ˆ๋ฆฌ, ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋”•์…”๋„ˆ๋ฆฌ, ๋ฆฌ์ŠคํŠธ - true
โ€ข
์•„๋ฌด ๊ฐ’๋„ ์—†๋‹ค๋Š” ์˜๋ฏธ์ธ None - false
โ€ข
๋นˆ๋ฌธ์ž์—ด์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž์—ด - true
2) is vs ==
โ€ข
is: ๋ณ€์ˆ˜๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด itself ๋น„๊ต
โ€ข
==: ๋ณ€์ˆ˜๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด ๋‚ด ๊ฐ’ ๋น„๊ต
list1 = [1, 2, 3] list2 = [1, 2, 3] if list1 == list2: print("list1๊ณผ list2์˜ ๊ฐ’์€ ๊ฐ™์Šต๋‹ˆ๋‹ค.") if list1 is list2: print("ํ•˜์ง€๋งŒ list1๊ณผ list2๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.")
Python
๋ณต์‚ฌ
3) for in range vs enumerate
โ€ข
for in range
rainbow=["๋นจ","์ฃผ","๋…ธ","์ดˆ","ํŒŒ","๋‚จ","๋ณด"] for i in range(len(rainbow)): color = rainbow[i] print('{}๋ฒˆ์งธ ์ƒ‰์€ {}'.format(i+1,color))
Python
๋ณต์‚ฌ
4) enumerate
โ€ข
enumerate: for in range์˜ len ํ™œ์šฉ ๋ฐ color ๋ณ€์ˆ˜ ์„ ์–ธ์„ ์ถ•์•ฝํ•œ ํ˜•ํƒœ
rainbow=["๋นจ","์ฃผ","๋…ธ","์ดˆ","ํŒŒ","๋‚จ","๋ณด"] for i,color in enumerate(rainbow): print('{}๋ฒˆ์งธ ์ƒ‰์€ {}'.format(i+1,color))
Python
๋ณต์‚ฌ

3. ์ž๋ฃŒํ˜•

๊ฐ€. ์ž๋ฃŒ๊ตฌ์กฐ๋ณ„ ์‹œ๊ฐ„๋ณต์žก๋„ ๋ถ„์„

1) Set
โ€ข
์ ํ•ฉํ•œ ์‚ฌ์šฉ๋ฒ•
โ†’ ๊ฐ๊ฐ์˜ ๊ตฌ์„ฑ์š”์†Œ์— ๋Œ€ํ•œ ์กฐํšŒ๊ฐ€ ํ•„์š” ์—†๋Š” ๊ฒฝ์šฐ
โ†’ ์ž๋ฃŒ๊ตฌ์กฐ ๋‚ด ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ์œ ์ผํ•œ ๊ฐ’์ผ ๊ฒฝ์šฐ(์ค‘๋ณต๊ฐ’์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ)
โ†’ ๊ฐ๊ฐ์˜ ๊ตฌ์„ฑ์š”์†Œ์— ๋Œ€ํ•œ ํฌํ•จ์—ฌ๋ถ€ ํ™•์ธ ๋ฐ ์‚ฝ์ž…์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
โ€ข
ํฌํ•จ์—ฌ๋ถ€ ํ™•์ธ
โ†’ in or not in: O(1)
โ†’ Set์€ hashable ๊ฐ์ฒด์ด๋ฏ€๋กœ ํ•ด์‰ฌ๊ฐ’๊ณผ ๋‚ด๋ถ€ ๊ตฌ์„ฑ์š”์†Œ ๊ฐ„์˜ lookup table์ด ์ƒ์„ฑ๋˜๊ณ , ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํฌํ•จ์—ฌ๋ถ€๋ฅผ ์กฐํšŒํ•˜๋ฉด O(1) ๊ฒฐ๊ณผ ๋„์ถœ
โ†’ ๋‹จ, Set์˜ __hash__๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์•„์„œ ๋ชจ๋“  ๊ตฌ์„ฑ์š”์†Œ์˜ ํ•ด์‰ฌ๊ฐ’์ด ๋™์ผํ•˜๋‹ค๋ฉด lookup ํ…Œ์ด๋ธ”์„ ํ™œ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ํฌํ•จ์—ฌ๋ถ€์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)์ด ๋  ์ˆ˜ ์žˆ๋‹ค
โ€ข
์‚ฝ์ž…์‚ญ์ œ
โ†’ add, remove: O(1)
2) List
โ€ข
์ ํ•ฉํ•œ ์‚ฌ์šฉ๋ฒ•
โ†’ ๊ฐ ๊ตฌ์„ฑ์š”์†Œ์— ๋Œ€ํ•ด ์ธ๋ฑ์Šค ์กฐํšŒ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ
โ†’ ์ž๋ฃŒ๊ตฌ์กฐ ๋‚ด ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ์ค‘๋ณต๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ
โ€ข
์ธ๋ฑ์Šค ์กฐํšŒ
โ†’ list[x]: O(1)
โ€ข
ํฌํ•จ์—ฌ๋ถ€ ํ™•์ธ
โ†’ in or not in: O(n)
โ€ข
์‚ฝ์ž…์‚ญ์ œ
โ†’ insert, delete: O(n)
โ†’ append, pop: O(1)
3) Dict
โ€ข
์ ํ•ฉํ•œ ์‚ฌ์šฉ๋ฒ•
โ†’ ์ž๋ฃŒ๊ตฌ์กฐ ๋‚ด ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ key-value์˜ ์Œ์œผ๋กœ ๊ด€๋ฆฌ๋  ํ•„์š”๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
โ†’ ๊ตฌ์„ฑ์š”์†Œ์˜ ์กฐํšŒ ๋ฐ ๊ฐฑ์‹  ์ž‘์—…์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
โ€ข
์ธ๋ฑ์Šค ์กฐํšŒ
โ†’ dict[โ€™keyโ€™]: O(1)
โ€ข
ํฌํ•จ์—ฌ๋ถ€ ํ™•์ธ
โ†’ in or not in: O(1)
โ€ข
์‚ฝ์ž…์‚ญ์ œ
โ†’ del, store: O(1)

๋‚˜. List vs Dictionary

1) ๊ณตํ†ต์ 
Search
๊ณตํ†ต์ 
List
Dictionary
ํ˜ธ์ถœ
dict[ 'one' ]
๊ฐœ์ˆ˜ ํ™•์ธ
len( dict )
๊ฐ’ ํ™•์ธ
'two' in dict.keys( )
์ „๋ถ€ ์‚ญ์ œ
dict.clear( )
๊ฐ’ ์ถ”๊ฐ€
dict['three'] = 3
๊ฐ’์œผ๋กœ ์‚ญ์ œ
2) ์ฐจ์ด์ 
Search
List
Dictionary
์ˆœ์„œ
key๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์‚ญ์ œ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†๋‹ค
๊ฒฐํ•ฉ
dict1.update( dict2 )
3) Dictionary์—์„œ None ์ฒ˜๋ฆฌ
โ€ข
Dictionary์—์„œ Key ๊ฐ’์œผ๋กœ Value ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
โ†’ dict[โ€™oneโ€™]: key๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ
โ†’ dict.get(โ€™oneโ€™): key๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด None ๋ฐ˜ํ™˜
4) List comprehension
โ€ข
[ ๊ณ„์‚ฐ์‹ for๋ฌธ ]
โ†’ [ i*i for i in range(1,11) ]
โ€ข
[ ๊ณ„์‚ฐ์‹ for๋ฌธ ์กฐ๊ฑด๋ฌธ ]
โ†’ย [ i*i for i in range(1,11) if i % 2 == 0 ]
โ€ข
[ ๊ณ„์‚ฐ์‹ for๋ฌธ for๋ฌธ ]
โ†’ย [ ( x, y ) for x in range(15) for y in range(15) ]
5) Dictionary comprehension
โ€ข
{ ํ˜•์‹ for๋ฌธ }
students = ["ํƒœ์—ฐ", "์ง„์šฐ", "์ •ํ˜„", "ํ•˜๋Š˜", "์„ฑ์ง„"] # {'2๋ฒˆ': '์ •ํ˜„', '0๋ฒˆ': 'ํƒœ์—ฐ', '1๋ฒˆ': '์ง„์šฐ', '4๋ฒˆ': '์„ฑ์ง„', '3๋ฒˆ': 'ํ•˜๋Š˜'} {"{}๋ฒˆ".format(number):name for number, name in enumerate(students)}
Python
๋ณต์‚ฌ
โ€ข
{ํ‚ค:๊ฐ’ for in zip}
students = ["ํƒœ์—ฐ", "์ง„์šฐ", "์ •ํ˜„", "ํ•˜๋Š˜", "์„ฑ์ง„"] scores = [85, 92, 78, 90, 100] # {'์„ฑ์ง„': 100, '์ง„์šฐ': 92, 'ํ•˜๋Š˜': 90, 'ํƒœ์—ฐ': 85, '์ •ํ˜„': 78} result = {x : y for x, y in zip(students, scores)}
Python
๋ณต์‚ฌ
6) List Slicing
โ€ข
์กฐํšŒ
list1 = list(range(20)) # new_list 5, 8, 11, 14 new_list = list1[5:15:3] # reverse_list 17, 13, 9, 5 reverse_list = list1[17:4:-4]
Python
๋ณต์‚ฌ
โ€ข
๊ฐฑ์‹ 
list1 = [0, 1, 2, 3, 4, 5] list1[1:4] = [11, 22, 33] # list1 = [0, 11, 22, 33, 4, 5] list2 = [0, 1, 2, 3, 4, 5] del list2[1:4] # list2 = [0, 4, 5]๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
Python
๋ณต์‚ฌ
7) packing, unpacking
โ€ข
packing: ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฐ’์„ ๋„ฃ๋Š” ๊ฒƒ
โ€ข
unpacking: ํŒจํ‚น๋œ ๋ณ€์ˆ˜์—์„œ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฐ’์„ ๊บผ๋‚ด์˜ค๋Š” ๊ฒƒ
c = (3, 4) d, e = c # c์˜ ๊ฐ’์„ ์–ธํŒจํ‚นํ•˜์—ฌ d, e์— ๊ฐ’์„ ๋„ฃ์—ˆ๋‹ค f = d, e # ๋ณ€์ˆ˜ d์™€ e๋ฅผ f์— ํŒจํ‚น
Python
๋ณต์‚ฌ

๋‹ค. ๋ฌธ์ž์—ด

1) ๊ฒ€์ƒ‰
โ€ข
String ์ธ๋ฑ์Šค ์ ‘๊ทผ
>>> s = 'abcdef' >>> s[3] 'd'
Python
๋ณต์‚ฌ
โ€ข
์ „๋ฐฉ์ผ์น˜: startwith()
โ€ข
ํ›„๋ฐฉ์ผ์น˜: endwith()
โ€ข
๋ถ€๋ถ„์ผ์น˜: not in, in
โ€ข
์™„์ „์ผ์น˜: ==, !=
โ†’ is์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด์ˆ˜์ค€์˜ ์ผ์น˜์—ฌ๋ถ€ ํ™•์ธ
2) ํŠน์ • ๋ฌธ์ž ์ œ๊ฑฐ
โ€ข
๋ฌธ์ž์—ด์—์„œ ๋Œ€๊ด„ํ˜ธ ์ œ๊ฑฐ
โ†’ strip์˜ ๊ฒฝ์šฐ ์–‘์ชฝ์—์„œ ํ•œ ๋ฒˆ์”ฉ ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•œ๋‹ค
target_string = "['pig', 'cow'], ['pig', 'cow']" refined_string = target_string.strip("[" "]") print(refined_string) # 'pig', 'cow'], ['pig', 'cow'
Python
๋ณต์‚ฌ
3) ํŠน์ • ๋ฌธ์ž ์น˜ํ™˜
โ€ข
์ค‘๋ณต ์กด์žฌํ•˜๋Š” ํŠน์ • ๋ฌธ์ž๋ฅผ ๋ฌธ์ž์—ด์—์„œ ๋ชจ๋‘ ์ œ๊ฑฐ
target_string = "['pig', 'cow'], ['pig', 'cow']" refined_string = target_string.replace("[", "") refined_string = refined_string.replace("]", "") print(refined_string) # 'pig', 'cow', 'pig', 'cow'
Python
๋ณต์‚ฌ
4) ํŠน์ • ๋ฌธ์ž ๊ธฐ์ค€ ๋ฌธ์ž์—ด โ†’ ๋ฆฌ์ŠคํŠธ ๋ณ€๊ฒฝ
โ€ข
์‰ผํ‘œ๊ธฐ์ค€ ๋ฌธ์ž์—ด ๋‚ด ๋ฌธ์ž๋ฅผ ์š”์†Œ๋กœํ•˜๋Š” ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
target_string = "[pig,cow],[pig,cow]" refined_string = target_string.replace("[", "") refined_string = refined_string.replace("]", "") changed_list = refined_string.split(",") print(changed_list) # ['pig', 'cow', 'pig', 'cow']
Python
๋ณต์‚ฌ

4. ํ•จ์ˆ˜

๊ฐ€. ๋งค๊ฐœ๋ณ€์ˆ˜

1) *args
โ€ข
๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •ํ•ด์ง€์ง€ ์•Š์•˜์„ ๋•Œ ์‚ฌ์šฉ
โ€ข
ํŠœํ”Œ ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋จ
def print_param(*args): print args for p in args: print p print_param('a', 'b', 'c', 'd') #('a', 'b', 'c', 'd') #a #b #c #d ์ถœ์ฒ˜: https://jhproject.tistory.com/109 [JH PROJECT]
Python
๋ณต์‚ฌ
2) **kwargs
โ€ข
๋งค๊ฐœ๋ณ€์ˆ˜ ๋ช… ํ•จ๊ป˜ ์ „๋‹ฌ ๊ฐ€๋Šฅ
โ€ข
๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์ „๋‹ฌ
def print_param2(**kwargs): print kwargs print kwargs.keys() print kwargs.values() for name, value in kwargs.items(): print "%s : %s" % (name, value) print_param2(first = 'a', second = 'b', third = 'c', fourth = 'd') #{'second': 'b', 'fourth': 'd', 'third': 'c', 'first': 'a'} #['second', 'fourth', 'third', 'first'] #['b', 'd', 'c', 'a'] #second : b #fourth : d #third : c #first : a ์ถœ์ฒ˜: https://jhproject.tistory.com/109 [JH PROJECT]
Python
๋ณต์‚ฌ

5. ๊ฐ์ฒด์™€ ํด๋ž˜์Šค

๊ฐ€. ๊ฐ„๋‹จํ•œ ๊ฐ์ฒด

โ€ข
ํด๋ž˜์Šค ์˜ˆ์ œ
class Human(): def __init__(self, name, weight): self.name = name self.weight = weight def __str__(self): return "{} (๋ชธ๋ฌด๊ฒŒ {}kg)".format(self.name, self.weight) def eat(self): self.weight += 0.1 print("{}๊ฐ€ ๋จน์–ด์„œ {}kg์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.".format(self.name, self.weight)) def walk(self): self.weight -= 0.1 print("{}๊ฐ€ ๊ฑธ์–ด์„œ {}kg์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.".format(self.name, self.weight)) person = Human("MJ", 85) person.walk() # MJ๊ฐ€ ๊ฑธ์–ด์„œ 84.9kg์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. person.walk() # MJ๊ฐ€ ๊ฑธ์–ด์„œ 84.80000000000001kg์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. person.eat() # MJ๊ฐ€ ๋จน์–ด์„œ 84.9kg์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. print(person) # MJ (๋ชธ๋ฌด๊ฒŒ 84.9kg)
Python
๋ณต์‚ฌ
1) ์†์„ฑ
โ€ข
ํด๋ž˜์Šค์˜ ์†์„ฑ์€ ์†์„ฑ๊ฐ’๊ณผ ๋ฉ”์†Œ๋“œ๋กœ ๊ตฌ์„ฑ๋จ
โ€ข
์†์„ฑ๊ฐ’์€ ๋ฐ์ดํ„ฐ์ด๊ณ  ๋ฉ”์†Œ๋“œ๋Š” ํ•จ์ˆ˜์ž„
2) ๋ฉ”์„œ๋“œ
โ€ข
์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ self๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค ์ž์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด
class Car(): def run(self): print("{}๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.".format(self.name)) taxi = Car() taxi.name = "ํƒ์‹œ" taxi.run()
Python
๋ณต์‚ฌ
โ€ข
static method: ํด๋ž˜์Šค๋‚˜ ์ธ์Šคํ„ด์Šค์˜ ์†์„ฑ์— ์˜์กดํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋กœ ํ•จ์ˆ˜ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ
โ†’ staticmethod ์žฅ์‹์ž๋ฅผ ๋ถ™์—ฌ์•ผ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์—์„œ static method ํ˜ธ์ถœ ๊ฐ€๋Šฅ
class Human(): def __init__(self, name, weight): self.name = name self.weight = weight @staticmethod def say_hello(): print("hello")
Python
๋ณต์‚ฌ
3) ๋งค์ง ๋ฉ”์„œ๋“œ
โ€ข
_ _init_ _: ํด๋ž˜์Šค์˜ ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
โ†’ Java์˜ toString ๊ฐœ๋…
โ€ข
_ _str_ _: ํด๋ž˜์Šค์— ๋Œ€ํ•œ default ๋ฌธ์ž์—ด ์„ค์ •
โ€ข
_ _doc_ _: ํด๋ž˜์Šค ์ „๋ฐ˜์— ๋Œ€ํ•ด ๋ช…์‹œํ•œ ์ฃผ์„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
4) namespace
โ€ข
์ •์˜: ๊ฐœ์ฒด๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„
โ†’ __dict__ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด์˜ namespace๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
โ€ข
ํด๋ž˜์Šค ๊ฐ์ฒด์™€ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์— ๋Œ€ํ•œ namespace ํ™•์ธ
โ€ข
๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ ์œ„ํ•ด ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์˜ namespace์—๋Š” ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์˜ ์†์„ฑ๊ฐ’๋งŒ์„ ์ •์˜ํ•จ
โ†’ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ ๋ฐ ํด๋ž˜์Šค ์†์„ฑ์€ ๋ชจ๋‘ ํด๋ž˜์Šค namespace์— ์ •์˜๋จ
โ†’ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์˜ namespace์— ์—†๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๊ณ , ํด๋ž˜์Šค ๊ฐ์ฒด์˜ namesplace๋ฅผ ํ™•์ธํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ™•์ธํ•˜์—ฌ ์‹คํ–‰ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค์ž„
โ†’ ์œ„์™€ ๊ฐ™์ด ๋™์ ์ธ ์‹คํ–‰์ˆœ์„œ๋ฅผ ๊ฐ–๊ณ  ์žˆ์œผ๋ฏ€๋กœ ํŒŒ์ด์ฌ์„ ๋™์ ์ธ ์–ธ์–ด๋กœ ํ‘œํ˜„ํ•จ
person.__dict__ {'name': 'MJ', 'weight': 84.9} Human.__dict__ mappingproxy({'__module__': '__main__', '__init__': <function Human.__init__ at 0x1025b10d0>, '__str__': <function Human.__str__ at 0x1025b11f0>, 'eat': <function Human.eat at 0x1025b1310>, 'walk': <function Human.walk at 0x1025b13a0>, '__dict__': <attribute '__dict__' of 'Human' objects>, '__weakref__': <attribute '__weakref__' of 'Human' objects>, '__doc__': None})
Python
๋ณต์‚ฌ
5) self vs cls
โ€ข
self ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด ๊ทธ ์ž์ฒด
โ†’ self์˜ id๊ฐ’๊ณผ ์ธ์Šคํ„ด์Šค์˜ id๊ฐ’์ด ๋™์ผ
โ€ข
cls ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ๊ฐ์ฒด ๊ทธ ์ž์ฒด
6) composition
โ€ข
์ƒ์œ„ ๊ฐ์ฒด ์ž์ฒด๋ฅผ ์ƒ์† ๋ฐ›์ง€ ์•Š๊ณ  ์ผ๋ถ€ ๋ฉ”์†Œ๋“œ๋งŒ ์ƒ์† ๋ฐ›๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ ํ™œ์šฉ
class A(object): def __init__(self, a): self.a = a def do_work(self, a): print(f'{a} do_work') class C: def __init__(self, a): self.a = a self.A = A(a) def do_work(self, h): print(h) return self.A.do_work(self.a) c = C("k") c.do_work("t") # t # k do_work
Python
๋ณต์‚ฌ
7) ์ ‘๊ทผ์ œ์–ด์ž
โ€ข
ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ๊ณต์‹์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” ์ ‘๊ทผ์ œ์–ด์ž๋Š” public๊ณผ private๋งŒ ์กด์žฌํ•จ
โ†’ public: ์†์„ฑ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ ‘๊ทผ์„ ํ—ˆ์šฉ
โ†’ private: ์†์„ฑ์ด private์œผ๋กœ ์ •์˜๋œ class ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ ํ—ˆ์šฉ
โ†’ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ๋„ private์œผ๋กœ ์ •์˜๋œ ์†์„ฑ ์ ‘๊ทผ ๋ถˆ๊ฐ€
โ€ข
ํŒŒ์ด์ฌ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ์–ธ๋”๋ฐ” ํ•˜๋‚˜๋ฅผ protected ์ ‘๊ทผ์ œ์–ด์ž๋กœ ์‚ฌ์šฉํ•˜์ž๋Š” ์ผ๋ถ€์˜ ํ•ฉ์˜ ์กด์žฌ
โ†’ protected: ์†์„ฑ์ด protected๋กœ ์ •์˜๋œ class๋ฅผ ์ƒ์† ๋ฐ›์œผ๋ฉด ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, class ์™ธ๋ถ€์—์„œ๋Š” ์ ‘๊ทผ ๋ถˆ๊ฐ€
โ†’ ๋‹จ, ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ๊ณต์‹์ ์œผ๋กœ protected๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— ์–ธ๋”๋ฐ” ํ•˜๋‚˜๋กœ ์ •์˜๋œ ์†์„ฑ์€ class ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ
โ†’ ์ปค๋ฎค๋‹ˆํ‹ฐ ์ผ๋ถ€์—์„œ๋Š” ์–ธ๋”๋ฐ” ํ•˜๋‚˜๋ฅผ ์ž„์‹œ ๋ณ€์ˆ˜๋กœ ๋ณด์ž๋Š” ํ•ฉ์˜๋„ ์กด์žฌํ•จ
โ€ข
์ ‘๊ทผ์ œ์–ด์ž ์˜ˆ์‹œ
class A: def __init__(self, a, b): self.a = a self.__b = b def _test_protected(self, c): print(f'{self.a}, {c}') class B(A): def __init__(self, a, b, d): super().__init__(a, b) self._d = d def __test_private(self): print(f'{self.a}') i_a = A(1, 2) i_b = B(3, 4, 5) print(i_a.a) # 2 print(i_a._test_protected('hello')) # 1, hello print(i_b.a) # 3 print(i_b._d) # 5 print(i_b.__test_private('hello')) # AttributeError: 'B' object has no attribute '__test_private'
Python
๋ณต์‚ฌ

๋‚˜. ์ƒ์†

1) Super()
โ€ข
์ž์‹ ๊ฐ์ฒด์—์„œ ๋ถ€๋ชจ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
โ€ข
super์— ์ƒ์œ„ ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด ํ•ด๋‹น ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•จ
โ†’ ์ง€์ •ํ•œ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์œ„ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด AttributeError ๋ฐœ์ƒ
class A(object): def do_work(self): print('A์˜ do_work') class B(A): def do_work(self): print('B์˜ do_work') super(B, self).do_work() class C(B): def do_work(self): print(1) super(C, self).do_work() print(2) super(B, self).do_work() print(3) super(A, self).do_work # C ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ c = C() c.do_work() -- ๊ฒฐ๊ณผ -- 1 B์˜ do_work A์˜ do_work 2 A์˜ do_work 3 AttributeError: 'super' object has no attribute 'do_work'
Python
๋ณต์‚ฌ
example code from https://sirzzang.github.io/dev/Dev-python-super/
โ€ข
super์— ์ƒ์œ„ ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋ชจ๋“  ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•จ
class A(object): def do_work(self): print('A์˜ do_work') class B(A): def do_work(self): print('B์˜ do_work') super().do_work() class C(B): def do_work(self): print(1) super().do_work() print(2) super().do_work() print(3) super().do_work # C ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ c = C() c.do_work() -- ๊ฒฐ๊ณผ 1 B์˜ do_work A์˜ do_work 2 B์˜ do_work A์˜ do_work 3 B์˜ do_work A์˜ do_work
Plain Text
๋ณต์‚ฌ
example code from https://sirzzang.github.io/dev/Dev-python-super/

๋‹ค. Self

1) ์ •์˜
โ€ข
์ธ์Šคํ„ด์Šค ๊ฐ์ฒด ์ž์‹ ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ณ€์ˆ˜
โ†’ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ self ์ „๋‹ฌํ•ด์•ผ ํ•จ
2) ๋™์ž‘์›๋ฆฌ
โ€ข
a_car.exclaim() ํ˜ธ์ถœ์— ๋”ฐ๋ฅธ self์˜ ๋™์ž‘์›๋ฆฌ
โ†’ a_car ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํด๋ž˜์Šค ๊ฐ์ฒด ํƒ์ƒ‰
โ†’ a_car ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ Car ํด๋ž˜์Šค ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ์˜ self ๋ณ€์ˆ˜์— ํ• ๋‹น
โ†’ ๊ฒฐ๊ณผ์ ์œผ๋กœ a_car.exclaim() == Car.exclaim(a_car)
class Car: def exclaim(self): print("I'am a Car") a_car = Car() a_car.exclaim() # I'am a Car
Python
๋ณต์‚ฌ

๋ผ. Method Type

1) ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ
โ€ข
ํŠน์ • ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์—๋งŒ ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋ฉ”์„œ๋“œ
โ€ข
๋ฉ”์„œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ self(์ธ์Šคํ„ด์Šค ๊ฐ์ฒด ์ž์‹ ์„ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜)๋ฅผ ์ „๋‹ฌํ•จ
2) ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ
โ€ข
ํด๋ž˜์Šค ๊ฐ์ฒด ์ž์ฒด์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋ฉ”์„œ๋“œ
โ†’ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋ฉ”์„œ๋“œ
โ€ข
@classmethod๋ผ๋Š” decoration์ด ๋ถ™๊ณ  ์ฒซ๋ฒˆ์žฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ cls(ํด๋ž˜์Šค ๊ฐ์ฒด ์ž์‹ ์„ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜)๋ฅผ ์ „๋‹ฌํ•จ
>>> class A(): ... count = 0 ... def __init__(self): ... A.count += 1 ... @classmethod ... def class_kids(cls): ... print(cls.count) ... def instance_increase(self): ... self.count += 1 ... def instance_kids(self): ... print(self.count) ... >>> first_a = A() >>> second_a = A() >>> first_a.instance_increase() >>> A.class_kids() 2 >>> first_a.instance_kids() 3
Python
๋ณต์‚ฌ
3) ์ •์  ๋ฉ”์„œ๋“œ
โ€ข
ํด๋ž˜์Šค๋‚˜ ์ธ์Šคํ„ด์Šค์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์ง€๋งŒ ํŽธ๋ฆฌ์„ฑ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ
โ€ข
ํด๋ž˜์Šค๋‚˜ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ •์  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
โ€ข
@staticmethod๋ผ๋Š” decoration์ด ๋ถ™์Œ
>>> class Weapon(): ... @staticmethod ... def commercial(): ... print("Weapon! Wow!") ... >>> Weapon.commercial() Weapon! Wow!
Python
๋ณต์‚ฌ

๋งˆ. Mixin

1) ์ •์˜
โ€ข
์ผ๋ จ์˜ ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ์˜ ์ง‘ํ•ฉ
โ†’ ๊ฐ™์€ base class๋ฅผ ์ƒ์†ํ•˜์ง€ ์•Š๋Š” ๊ฐ์ฒด ๊ฐ„์— ํŠน์ • ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
2) ์˜ˆ์‹œ
โ€ข
Cow, Bull, Ox ํด๋ž˜์Šค๋Š” ๋ชจ๋‘ Cattle์ด๋ผ๋Š” base class๋ฅผ ์ƒ์†๋ฐ›์Œ
โ€ข
Bull, Ox๋Š” ์ˆ˜์†Œ์ด๋ฏ€๋กœ ๋‘ ๊ฐ์ฒด์—๋งŒ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌํ•จ
โ†’ ์†์„ฑ(์ •์•ก๋ฒˆํ˜ธ), ๋ฉ”์†Œ๋“œ(์ •์•ก์ฑ„์ทจ)๊ฐ€ Mixin์œผ๋กœ ์ •์˜๋˜์–ด Bull๊ณผ Ox ํด๋ž˜์Šค๊ฐ€ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ์Œ
class Cattle(object): def __init__(self, birth_date): self.birth_date = birth_date def get_age_in_days(): return today() - self.birth_date class MaleMixin(object): def __init__(self, seed_id): self.seed_id = seed_id def get_seed_id(): return self.seed_id class Cow(Cattle): pass class Bull(Cattle, MaleMixin): pass class Ox(Cattle, MaleMixin): pass
Python
๋ณต์‚ฌ

๋ฐ”. Hash

1) Object Hash
โ€ข
ํŠน์ • ๊ฐ์ฒด๋ฅผ Hashableํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด __hash__์™€ __eq__ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•ด์•ผํ•จ
โ€ข
๊ฐ์ฒด์˜
2) Hash ์‚ฌ์šฉ์ฒ˜
โ€ข
Set๊ณผ Dict์˜ ๊ตฌ์„ฑ์š”์†Œ์˜ ๊ฐ’๊ณผ ํ•ด์‰ฌ๊ฐ’์„ ๊ฐ€์ง€๊ณ  lookup table์„ ๊ตฌ์„ฑํ•˜์—ฌ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ธฐ๋ณธ๊ธฐ๋Šฅ(์กฐํšŒ, ๋น„๊ต ๋“ฑ)์— ํ™œ์šฉ๋จ
โ†’ ํ•ด์‰ฌ๊ฐ’์€ lookup table์—์„œ ํ‚ค๋กœ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ ํ•ด์‰ฌ๊ฐ’์ด ์ค‘๊ฐ„์— ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ ๋ถ€์ž‘์šฉ์„ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์Œ
โ†’ ํŠน์ • ๊ฐ์ฒด์—์„œ ํ•ด์‰ฌ ๊ฐ’ ์ƒ์„ฑ์— ํ† ๋Œ€๊ฐ€ ๋˜๋Š” ๊ฐ’์€ ํ•ด๋‹น ๊ฐ์ฒด์˜ lifecycle์ฒด์„œ ๋ถˆ๋ณ€์˜ ๊ฐ’์ด์–ด์•ผ ํ•จ
โ†’ ๊ฐ™์€ ๋งฅ๋ฝ์—์„œ python ๊ธฐ๋ณธ ์ž๋ฃŒํ˜• ์ค‘ immutable ๊ฐ์ฒด(tuple, string)๋Š” hashableํ•˜์ง€๋งŒ mutableํ•œ ๊ฐ์ฒด๋Š” unhashableํ•  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค

6. ๋ชจ๋“ˆ๊ณผ ํŒจํ‚ค์ง€

๊ฐ€. Decorator

1) Decorator ๊ฐœ์š”
โ€ข
๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ํ•จ์ˆ˜ ๋‚ด๋ถ€๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉํ•จ
โ€ข
๋™์ž‘๋ฐฉ์‹
โ†’ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๋ถ™์—ฌ์„œ ์ •์˜ํ•œ ํ•จ์ˆ˜๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ •์˜๋ถ€ ๋‚ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋จ
โ†’ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋œ ํ•จ์ˆ˜๋Š” wrapper ํ•จ์ˆ˜ ๋‚ด ์ง€์ •๋œ ์œ„์น˜์—์„œ ์‹คํ–‰๋จ
โ€ข
๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๊ธฐ๋ณธํ˜•ํƒœ ์ •์˜
# ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ •์˜ def decorator_function(function): def wrapper(): print("start test") function() print("end test") return wrapper # ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ถ™์ธ ํ•จ์ˆ˜ ์ •์˜ @decorator_function def test_decorator(): print("hello") # ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ถ™์ธ ํ•จ์ˆ˜ ์‹คํ–‰ test_decorator()
Python
๋ณต์‚ฌ
โ€ข
์˜ˆ์‹œ ์ฝ”๋“œ ์‹คํ–‰ ๋ฐ ์ถœ๋ ฅ
โ€ข
๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ์ƒ๋žตํ•œ ๋กœ์ง
โ†’ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•  ํ•จ์ˆ˜์˜ ์ •์˜๋ถ€์— ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ์žฅ์‹์ž๋กœ ํ‘œ์‹œํ•˜๋ฉด, ๋ณธ๋ฌธ์—์„œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜์˜ ์„ ์–ธ ๋ถ€๋ถ„์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Œ
# ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ •์˜ def decorator_function(function): def wrapper(): print("start test") function() print("end test") return wrapper def test_decorator(): print("hello") test_decorator = decorator_function(test_decorator) test_decorator()
Python
๋ณต์‚ฌ
2) ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ฐ˜ํ™˜๊ฐ’ ์ฒ˜๋ฆฌ
โ€ข
๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ถ™์ธ ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ, ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ •์˜ ๋ถ€๋ถ„์˜ wrapper ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํฌํ•จํ•˜์—ฌ ์ •์˜ํ•ด์•ผ ํ•จ
โ€ข
๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ถ™์ธ ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ
# ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ •์˜ def decorator_function(function): def wrapper(sid, pw, name): print("start: ", function.__name__) function(sid, pw, name) print("end: ", function.__name__) return wrapper # ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ถ™์ธ ํ•จ์ˆ˜ ์ •์˜ @decorator_function def test_decorator_with_params(sid, pw, name): print(f'hello: {name}, sid: {sid}, passward: {pw}') # ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ถ™์ธ ํ•จ์ˆ˜ ์‹คํ–‰ test_decorator_with_params(name="MJ", sid=123, pw=456)
Python
๋ณต์‚ฌ
โ€ข
์˜ˆ์‹œ ์ฝ”๋“œ ์‹คํ–‰ ๋ฐ ์ถœ๋ ฅ
3) ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํด๋ž˜์Šค
โ€ข
ํด๋ž˜์Šค ํ˜•ํƒœ๋กœ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ •์˜
# ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ •์˜ class decorator_class: def __init__(self, function): self.function = function def __call__(self, *args, **kargs): print("start: ", self.function.__name__) self.function(*args, **kargs) print("end: ", self.function.__name__) # ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ถ™์ธ ํ•จ์ˆ˜ ์ •์˜ @decorator_class def test_decorator_with_params(sid, pw, name): print(f'hello: {name}, sid: {sid}, passward: {pw}') # ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ถ™์ธ ํ•จ์ˆ˜ ์‹คํ–‰ test_decorator_with_params(name="MJ", sid=123, pw=456)
Python
๋ณต์‚ฌ
โ€ข
์˜ˆ์‹œ ์ฝ”๋“œ ์‹คํ–‰ ๋ฐ ์ถœ๋ ฅ

๋‚˜. __init__

1) ์šฉ๋„
โ€ข
๋””๋ ‰ํ† ๋ฆฌ ์ดํ•˜์˜ ๋ชจ๋“ˆ(.py)์„ ํŒจํ‚ค์ง€๋กœ ๊ด€๋ฆฌ
2) ์‚ฌ์šฉ๋ฒ• ๋น„๊ต
โ€ข
๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ
Project ใ…ฃ-- a_package ใ…ฃ ใ…ฃ-- __init__.py ใ…ฃ ใ…ฃ-- feature_a.py ใ…ฃ ใ…ฃ-- feature_b.py ใ…ฃ-- main.py
Plain Text
๋ณต์‚ฌ
โ€ข
empty __init__.py in a_package
# main.py import a_package.feature_a import a_package.feature_b a_package.feature_a.furnction_a() a_package.feature_b.furnction_b()
Python
๋ณต์‚ฌ
โ€ข
non empty __init__.py in a_package
# __init__.py from .feature_a import furnction_a from .feature_b import furnction_b
Python
๋ณต์‚ฌ
# main.py import a_package a_package.feature_a.furnction_a() a_package.feature_b.furnction_b()
Python
๋ณต์‚ฌ

๋‹ค. Path

1) path ํ•จ์ˆ˜
โ€ข
ํŒŒ์ผ์˜ ๊ฒฝ๋กœ ํ™•์ธ
import sys from pathlib import Path path = Path(__file__).resolve().__str__() print(f'path: {path}')
Python
๋ณต์‚ฌ
โ€ข
file์ด ์œ„์น˜ํ•œ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ๋ฅผ python path ์ถ”๊ฐ€
โ†’ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ: Path(__file__).resolve()
โ†’ ํŒŒ์ผ์˜ ๋ถ€๋ชจ(๋””๋ ‰ํ† ๋ฆฌ)์˜ ๊ฒฝ๋กœ: Path(__file__).resolve().parent
import sys from pathlib import Path path = Path(__file__).resolve().parent.__str__() sys.path.append(path) # python path์— ํŒŒ์ผ์˜ ๋ถ€๋ชจ ๊ฒฝ๋กœ ์ถ”๊ฐ€
Python
๋ณต์‚ฌ
2) package importing issue
โ€ข
์ƒํ™ฉ
โ†’ manager-api/FA/urls.py์—์„œ update_farms_from_grpc method ํ˜ธ์ถœ
from grpc_client.gclient import update_farms_from_grpc update_farms_from_grpc()
Python
๋ณต์‚ฌ
โ†’ manager-api/grpc_client/farm_pb2 ๊ฒฝ๋กœ์˜ farm_pb2 ๋ชจ๋“ˆ์„ ๋ชป์ฐพ๋Š” ์ด์Šˆ ๋ฐœ์ƒ
import farm_pb2 def update_farms_from_grpc(): with grpc.insecure_channel(GRPC_URL) as channel: stub = farm_pb2_grpc.FarmControllerStub(channel)
Python
๋ณต์‚ฌ
โ†’ ์—๋Ÿฌ ๋กœ๊ทธ
File "/Users/maxwell/PycharmProjects/manager-api/FA/urls.py", line 10, in <module> from grpc_client.gclient import update_farms_from_grpc File "/Users/maxwell/PycharmProjects/manager-api/grpc_client/gclient.py", line 9, in <module> import farm_pb2
Plain Text
๋ณต์‚ฌ
โ€ข
์›์ธ: python path์— manager-api/grpc_client ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ import farm_pb2 ๋งŒ์œผ๋กœ ํ•ด๋‹น ๋ชจ๋“ˆ์„ ๊ฒฝ๋กœ ์ƒ ์ฐพ์„ ์ˆ˜ ์—†์Œ
โ€ข
ํ•ด๊ฒฐ: python path์— manager-api/grpc_client ๊ฒฝ๋กœ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ import farm_pb2 ๋งŒ์œผ๋กœ ํ•ด๋‹น ๋ชจ๋“ˆ์„ ๊ฒฝ๋กœ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •
import sys from pathlib import Path path = Path(__file__).resolve().parent.__str__() sys.path.append(path) import farm_pb2 def update_farms_from_grpc(): with grpc.insecure_channel(GRPC_URL) as channel: stub = farm_pb2_grpc.FarmControllerStub(channel)
Python
๋ณต์‚ฌ

7. ๋ฐ์ดํ„ฐ ๊ธธ๋“ค์ด๊ธฐ

๊ฐ€. with

1) ๊ฐ์ฒด์˜ ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ
โ€ข
ํ™œ์šฉ: ํŠน์ • ๊ฐ์ฒด์˜ ์ƒ์• ์ฃผ๊ธฐ(์ปจํ…์ŠคํŠธ)๊ฐ€ ํ˜ธ์ถœ์— ๋”ฐ๋ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋๋‚œ๋‹ค๋ฉด, with ๊ตฌ๋ฌธ์œผ๋กœ ๊ฐ์‹ธ๋Š” ๊ฒƒ์€ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฉด์—์„œ ํšจ๊ณผ์ 
โ†’ ์˜ˆ์‹œ ์ฝ”๋“œ
async with session.get(url) as response: html = await response.text() soup = BeautifulSoup(html, "html.parser") cont_thumb = soup.find_all("div", "cont_thumb") for cont in cont_thumb: title = cont.find("p", "txt_thumb") if title is not None: print(title.text)
Python
๋ณต์‚ฌ
โ€ข
with ๊ตฌ๋ฌธ
with EXPR as VAR: BLOCK
Python
๋ณต์‚ฌ
code from https://peps.python.org/pep-0343/
โ€ข
with ๊ตฌ๋ฌธ ํ’€์ด
mgr = (EXPR) exit = type(mgr).__exit__ # Not calling it yet value = type(mgr).__enter__(mgr) exc = True try: try: VAR = value # Only if "as VAR" is present BLOCK except: # The exceptional case is handled here exc = False if not exit(mgr, *sys.exc_info()): raise # The exception is swallowed if exit() returns true finally: # The normal and non-local-goto cases are handled here if exc: exit(mgr, None, None, None)
Python
๋ณต์‚ฌ
code from code from https://peps.python.org/pep-0343/
2) File ์ฒ˜๋ฆฌ
โ€ข
with ๊ตฌ๋ฌธ์€ ํŠน์ • ๋ฉ”์†Œ๋“œ๋‚˜ ํด๋ž˜์Šค ๋‚ด ์„ ์–ธ๋œ ์ž์› ํ•ด์ œ ๊ธฐ๋Šฅ์„ ์ž๋™์œผ๋กœ ํ˜ธ์ถœํ•˜์—ฌ ์ž์›์˜ ์ƒ์„ฑ, ์‚ฌ์šฉ, ์†Œ๋ฉธ ๊ณผ์ •์„ ์†์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ
โ€ข
file stream ์ฒ˜๋ฆฌํ•  ๋•Œ, ์ž๋™์œผ๋กœ close ๋ฉ”์†Œ๋“œ๋ฅผ ์ž๋™์œผ๋กœ ํ˜ธ์ถœํ•จ
# fp-test.json ํŒŒ์ผ์„ ์—ด๊ณ  json.dump ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ๋’ค close ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ with open('fp-test.json', 'w') as f: json.dump(fp_dict, f, ensure_ascii=False)
Python
๋ณต์‚ฌ

๋‚˜. JSON ์ฒ˜๋ฆฌ

1) JSON๊ณผ Python์˜ ํ˜ธํ™˜
โ€ข
JSON(JavaScript Object Notation)์€ Python๊ณผ์˜ ๋ฐ์ดํ„ฐ ํ˜ธํ™˜์— ์ ํ•ฉํ•จ
โ†’ JavaScript์™€ Python์€ ๊ฐ™์€ C-family ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋ฏ€๋กœ ์ƒํ˜ธ ๋ฐ์ดํ„ฐํ˜ธํ™˜์— ์ ํ•ฉํ•จ
โ€ข
์ธ์ฝ”๋”ฉ(or ์ง๋ ฌํ™”), ๋””์ฝ”๋”ฉ(or ์—ญ์ง๋ ฌํ™”) ๊ฐ„ ๋ฐ์ดํ„ฐ ํ˜ธํ™˜ ์ฐธ๊ณ 
2) ์ง๋ ฌํ™”(Python โ†’ JSON)
โ€ข
json.dump(): ๋””์Šคํฌ
โ€ข
json.dumps(): ๋ฉ”๋ชจ๋ฆฌ
3) ์—ญ์ง๋ ฌํ™”(JSON โ†’ Python)
โ€ข
json.load(): ๋””์Šคํฌ
โ€ข
json.loads(): ๋ฉ”๋ชจ๋ฆฌ

8. ๋‚ ์งœ์™€ ์‹œ๊ฐ„

๊ฐ€. datetime

1) ๊ธฐ๋ณธ ์‚ฌ์šฉ
โ€ข
ํŠน์ • ์กฐ๊ฑด ์ดํ›„์˜ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ๊ณ„์‚ฐ
addtime = datetime.timedelta(days = 10) datetime.datetime.now() + addtime # 10์ผ ํ›„ datetime.datetime.now() - addtime # 10์ผ ์ „ thedate = datetime.datetime.now().replace(hour = 10, minute=0, second = 0) + datetime.timedelta(days = 3) # 3์ผ ํ›„ 10์‹œ ์ •๊ฐ
Python
๋ณต์‚ฌ
2) str to datetime
โ€ข
๋ฌธ์ž์—ด์„ datetime์œผ๋กœ ๋ณ€ํ™˜
โ†’ JSON์œผ๋กœ ๋ฐ›์€ date๋ฅผ datetime ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ ํ›„ ๋‚ ์งœ ๊ณ„์‚ฐ์‹œ ํ•„์š”
from datetime import datetime datetime.strptime("2018-01-31", "%Y-%m-%d")
Plain Text
๋ณต์‚ฌ

๋‚˜. date

1) today
โ€ข
datetime์˜ now()์™€ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ
from datetime import date date.today() - obj.latest_work_date
Plain Text
๋ณต์‚ฌ
2) days
โ€ข
datetime.date ํƒ€์ž… ๊ฐ„ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์ •์ˆ˜๋กœ ๋ฐ›๋Š”๋ฐ ์‚ฌ์šฉ
(date.today() - obj.next_work_date) "exceeded_days": "-86400.0" (date.today() - obj.next_work_date).days "exceeded_days": -1
Plain Text
๋ณต์‚ฌ
3) str to date
โ€ข
๋ฌธ์ž์—ด์„ date type์œผ๋กœ ๋ณ€ํ™˜
โ€ข
datetime.strptime์—์„œ date() ์ถ”๊ฐ€ํ˜ธ์ถœํ•˜์—ฌ ๋ณ€ํ™˜
datetime.strptime(str, '%Y-%m-%d').date()
Python
๋ณต์‚ฌ
4) date to str
โ€ข
date ํƒ€์ž…์„ string์œผ๋กœ ๋ณ€ํ™˜
from datetime import date test_date = date.today() test_date_str = test_date.strptime('%Y-%m-%d')
Python
๋ณต์‚ฌ
5) ๋‘ ๋‚ ์งœ ์‚ฌ์ด์˜ ๋‚ ์งœ ์ƒ์„ฑ
โ€ข
timedelta๋ฅผ ํ™œ์šฉ
def _generate_dates(date__gte, date__lte): start_date = datetime.strptime(date__gte, "%Y-%m-%d").date() end_date = datetime.strptime(date__lte, "%Y-%m-%d").date() delta = end_date - start_date dates = [start_date + timedelta(days=i) for i in range(delta.days + 1)] return dates
Python
๋ณต์‚ฌ

9. Collections

๊ฐ€. Counter

1) ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•œ ์š”์†Œ ์ฐพ๊ธฐ
โ€ข
n๊ฐœ์˜ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•œ ์š”์†Œ ์ฐพ๊ธฐ: Counter().most_common(n)
Counter('abracadabra').most_common(3) [('a', 5), ('b', 2), ('r', 2)]
Python
๋ณต์‚ฌ
def mostCommonWord(self, paragraph: str, banned: Llst[str]) -> str: words = [word for word In re.sub(r'[^Xw]', ' ', paragraph).lower().split() if word not in banned] counts = collections.Counter(words) return counts.most_common(1)[0][0]
Python
๋ณต์‚ฌ

๋‚˜. defaultdict

์กด์žฌํ•˜์ง€ ์•Š๋Š” key๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด key ์กด์žฌ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ๋ฅผ ์ƒ๋žตํ•  ๋•Œ, defaultdict๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค
1) difaultdict(int)
โ€ข
key:value ์Œ์—์„œ ์ง€์ •ํ•˜์ง€ ์•Š์€ ํ‚ค์˜ value๋ฅผ ์กฐํšŒํ•˜๋ฉด ๊ธฐ๋ณธ๊ฐ’์ด 0์ด ๋œ๋‹ค.
int_dict = collections.defaultdict(int) print(int_idct[key_1]) // 0
Python
๋ณต์‚ฌ
2) difaultdict(list)
โ€ข
key:value ์ƒ์—์„œ value์˜ ๊ธฐ๋ณธ๊ฐ’์ด ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋œ๋‹ค.
list_dict = collections.defaultdict(list) print(list_dict[key_1]) // []
Python
๋ณต์‚ฌ
3) difaultdict(set)
list_dict = collections.defaultdict(set) print(list_dict[key_1]) // set()
Python
๋ณต์‚ฌ

Reference

โ€ข
Introducing Python 2nd Edit, Bill Lubanovic
โ€ข
ํŒŒ์ด์ฌ ์ž…๋ฌธ, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, https://programmers.co.kr/learn/courses/2#introduction
โ€ข
ํ•จ์ˆ˜-๋งค๊ฐœ๋ณ€์ˆ˜, https://jhproject.tistory.com/109
โ€ข
โ€ข
Json ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, https://rfriend.tistory.com/474
โ€ข
์ž๋ฃŒ๊ตฌ์กฐ๋ณ„ ์‹œ๊ฐ„๋ณต์žก๋„, https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt
โ€ข
vscode ide ์„ค์ •, https://earth-95.tistory.com/85