์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/23

 

[python ์‹ฌํ™”] 6. ์ž๋ฃŒํ˜• (์ง€๋Šฅํ˜• ๋ฆฌ์ŠคํŠธ, ์ œ๋„ˆ๋ ˆ์ดํ„ฐ, ๊ฐ€๋ณ€๋ถˆ๋ณ€, ์ •๋ ฌ)

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/22 https://silvercoding.tistory.com/21 https://silvercoding.tistory.com/20 https://silvercoding.tistory.com/19..

silvercoding.tistory.com

 

 

- ํ•ด์‹œํ…Œ์ด๋ธ” (hashtable) 

ํ•ด์‹œํ…Œ์ด๋ธ”์€ ์ž‘์€ ๋ฆฌ์†Œ์Šค๋กœ ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•œ๋‹ค. 

ํ•ด์‹œ ํ…Œ์ด๋ธ”์€ key์™€ value๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. 

 

*** Hash๊ฐ’์„ ํ™•์ธ ํ•จ์œผ๋กœ์จ ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜๋Š”์ง€ ์•ˆ๋˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Œ . 

# Hash ๊ฐ’ ํ™•์ธ  ---> ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜๋Š”์ง€ ์•ˆ๋˜๋Š”์ง€ , 
t1 = (10, 20, (30, 40, 50))
t2 = (10, 20, [30, 40, 50])

print('EX1-2 - ', hash(t1))
# print('EX1-3 - ', hash(t2))  #---> ๋ฆฌ์ŠคํŠธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด์‰ฌ ์“ธ ์ˆ˜ ์—†์Œ 

 

 

 

- ์ง€๋Šฅํ˜• ๋”•์…”๋„ˆ๋ฆฌ (Comprehending Dict) 

(์˜ˆ์ œ) ๊ตญ๊ฐ€์ฝ”๋“œ csv ํŒŒ์ผ -> tuple ๋ณ€ํ™˜ -> Dictionary ๋ณ€ํ™˜ 

(1) csv ํŒŒ์ผ ๋ถˆ๋Ÿฌ์™€์„œ tuple๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ( ํŒŒ์ผ: ํŒจ์บ  ์ œ๊ณต

import csv
from os import kill
from typing import Counter 

# ์™ธ๋ถ€ CSV TO List of tuple  
with open('./resources/test1.csv', 'r', encoding='UTF-8') as f:
    temp = csv.reader(f)
    # Header Skip
    next(temp)
    print()
    # ๋ณ€ํ™˜ 
    NA_CODES = [tuple(x) for x in temp]

print('EX2-1 - ')
print(NA_CODES)

๋‘์ค„๊นŒ์ง€๋งŒ ์บก์ณ. ์ตœ์ข…์ ์œผ๋กœ ๋‚˜๋ผ ์ฝ”๋“œ ํŠœํ”Œ์ด ๋“ค์–ด๊ฐ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. 

 

 

(2) Dictionary ๋กœ ๋ณ€ํ™˜ (์ง€๋Šฅํ˜• Dictionary ์‚ฌ์šฉ) 

n_code1 = {country: code for country, code in NA_CODES}
n_code2 = {country.upper() : code for country, code in NA_CODES}

n_code2๋Š” key๊ฐ’์„ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ๊นŒ์ง€ ํ•œ ๊ฒƒ. 

 

 

 

 

 

- Dict Setdefault  

source = (('k1', 'val1'),
            ('k1', 'val2'),
            ('k2', 'val3'),
            ('k2', 'val4'),
            ('k2', 'val5'))

new_dict1 = {}
new_dict2 = {}

(1) No use setdefault 

for k, v in source:
    if k in new_dict1:
        new_dict1[k].append(v)
    else:
        new_dict1[k] = [v]

print('EX3-1 - ', new_dict1)

์ค‘๋ณต๋˜๋Š” key๊ฐ€ ์žˆ์œผ๋ฉด, ๋ฆฌ์ŠคํŠธ์— appendํ•ด์„œ dict๋ฅผ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ์ด๋‹ค. 

 

 

(2) Use setdefault ( ์ด๊ฒƒ์˜ ์„ฑ๋Šฅ์ด ๋” ์ข‹๋‹ค๊ณ  ํ•œ๋‹ค. ) 

for k, v in source:
    new_dict2.setdefault(k, []).append(v)

print('EX3-2 - ', new_dict2)

setdefault๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด if๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•œ์ค„๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

 

 

 

 

- ์‚ฌ์šฉ์ž ์ •์˜ dict ์ƒ์† 

class UserDict(dict):
    def __missing__(self, key):
        print('Called : __missing__')
        if isinstance(key, str):
            raise KeyError(key)
        return self[str(key)]

    def get(self, key, default=None):
        print("Called : __getitem__")
        try:
            return self[key]
        except KeyError:
            return default

    def __contains__(self, key):
        print("Called : __contains__")
        return key in self.keys() or str(key) in self.keys()

dict๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

user_dict1 = UserDict(one=1, two=2)
user_dict2 = UserDict({"one" : 1, "two" : 2})
user_dict3 = UserDict([('one', 1), ('two', 2)])

์ด๋ ‡๊ฒŒ ๋”•์…”๋„ˆ๋ฆฌ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

print('EX4-1 - ', user_dict1, user_dict2, user_dict3)
print('EX4-2 - ', user_dict2.get("two"))
print('EX4-3 - ', 'one' in user_dict3)
# print('EX4-4 - ', user_dict3['three'])
print('EX4-5 - ', user_dict3.get('three'))
print('EX4-6 - ', 'three' in user_dict3)

 

์ฃผ์„์ฒ˜๋ฆฌํ•œ EX4-4 ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด 

__missing__ ์ด ํ˜ธ์ถœ๋˜๋ฉด์„œ ์œ„์—์„œ ์ •์˜ํ–ˆ๋˜ ํ‚ค์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค. 

 

 

 

 

- Immutable Dict 

from types import MappingProxyType

d = {'key1' : 'TEST1'}
# Read Only
d_frozen = MappingProxyType(d)

print('EX5-1 - ', d, id(d))
print('EX5-2 - ', d_frozen, id(d_frozen))
print('EX5-3 - ', d is d_frozen, d == d_frozen)

# d_frozen['key1'] = 'TEST2'  # ===> ๋ฐ”๋€Œ์ง€ ์•Š์Œ 

# ์›๋ณธ์€ ์ˆ˜์ • ๊ฐ€๋Šฅ 
d['key2'] = 'TEST2'
print('EX5-4 - ', d)

 

d์™€ d_frozen ์€ ๋‹ค๋ฅธ ๊ฐ์ฒด (๋‹ค๋ฅธ id) ์ด๊ณ , ๊ฐ’(๋‚ด์šฉ)์€ ๊ฐ™๋‹ค๊ณ  ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

d_frozen์„ ์ˆ˜์ •ํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. ์ฝ๊ธฐ๋งŒ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด๋‹ค. 

์›๋ณธ์ธ d๋Š” ์ˆ˜์ •(์ถ”๊ฐ€, ์‚ญ์ œ, ๋ณ€๊ฒฝ)์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

- Set 

s1 = {'Apple', 'Orange', 'Apple', 'Orange', 'Kiwi'}
s2 = set(['Apple', 'Orange', 'Apple', 'Orange', 'Kiwi'])
s3 = {3}
s4 = set()  # Not {}
s5 = frozenset({'Apple', 'Orange', 'Apple', 'Orange', 'Kiwi'})

ํ•œ๊ฐ€์ง€ ์ฃผ์˜ํ•  ์ ์€ ๋น„์–ด์žˆ๋Š” set๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋• set() ์„ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค. {} ์œผ๋กœ ์ƒ์„ฑํ•˜๋ฉด dict ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. 

# ์ถ”๊ฐ€ 
s1.add('Melon')

# ์ถ”๊ฐ€ ๋ถˆ๊ฐ€ 
# s5.add('Melon')

print('EX6-1 - ',  s1, type(s1))
print('EX6-2 - ',  s2, type(s2))
print('EX6-3 - ',  s3, type(s3))
print('EX6-4 - ',  s4, type(s4))
print('EX6-5 - ',  s5, type(s5))

Immutable Dict์™€ ๊ฐ™์ด frozenset์œผ๋กœ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

- ์„ ์–ธ ์ตœ์ ํ™” 

a = {5}  # ---> ์ด๊ฒŒ ๋” ๋น ๋ฅด๋‹ค. 
b = set([10])

 *** ์ฆ๋ช… 

from dis import dis 
print('EX6-5 - ')
print(dis('{10}'))

print('EX6-6 - ')
print(dis("set([10])"))

dis๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹คํ–‰๋˜๋Š” ์ˆœ์„œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

{10} ์˜ ์ ˆ์ฐจ๊ฐ€ ๋” ์ ๋‹ค!

 

 

- ์ง€๋Šฅํ˜• ์ง‘ํ•ฉ (Comprehending Set) 

from unicodedata import name

print('EX7-1 - ')
print({name(chr(i), "") for i in range(0, 256)})

๋”•์…”๋„ˆ๋ฆฌ์™€ ๋น„์Šทํ•˜๊ฒŒ for๋ฌธ์„ {}์•ˆ์— ๋„ฃ์–ด์ฃผ์–ด ์ง€๋Šฅํ˜• ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/22

 

[python ์‹ฌํ™”] 5. Special Method (Magic Method)

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/21 https://silvercoding.tistory.com/20 https://silvercoding.tistory.com/19 https://silvercoding.tistory.com/18..

silvercoding.tistory.com

 

 

  • ์ปจํ…Œ์ด๋„ˆ (Container) : ์„œ๋กœ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜• ํฌํ•จ ๊ฐ€๋Šฅ 

list, tuple, collections.deuqe  (ex) list --->[1, 3.0, 'hi']

  • Flat : ํ•œ๊ฐœ์˜ ์ž๋ฃŒํ˜•๋งŒ์„ ํฌํ•จ 

str, bytes, bytearray, array.array, memoryview 

  • ๊ฐ€๋ณ€ 

list, bytearray, array.array, memoryview, deque

  • ๋ถˆ๋ณ€ 

tuple, str, bytes 

 

 

๋‚˜๋ฆ„ ํŒŒ์ด์ฌ์„ ๋ฐฐ์šฐ๋Š” ์ „๊ณต์ž์ด์ง€๋งŒ ์‹ฌํ™”๊ณผ์ • ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉฐ ์•„์ง ๋ชจ๋ฅด๋Š” ๊ฒŒ ์ •๋ง ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค. 

์œ„์˜ ์ž๋ฃŒํ˜•๋“ค๋งŒ ๋ด๋„! 

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ง€๋Šฅํ˜• ๋ฆฌ์ŠคํŠธ, ์ œ๋„ˆ๋ ˆ์ดํ„ฐ, ํŠœํ”Œ, ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋‚ด์šฉ์„ ๊นŠ๊ฒŒ ๋“ค์–ด๊ฐ„๋‹ค. 

 

 

- ์ง€๋Šฅํ˜• ๋ฆฌ์ŠคํŠธ (Comprehending Lists) 

*** None comprehending LIsts 

chars = '!@#$%^&*()_+'
codes1 = []

for s in chars:
    # ์œ ๋‹ˆ์ฝ”๋“œ ๋ณ€ํ™˜
    codes1.append(ord(s))

 

์ง€๋Šฅํ˜• ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” ์˜ˆ์‹œ์ด๋‹ค. 

 

*** Comprehending Lists 

์—„์ฒญ ํฐ ์˜ํ–ฅ์„ ๋ผ์น˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ๋น…๋ฐ์ดํ„ฐ ๋ถ„์•ผ์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๊ฐ€ ํ˜„์ €ํ•˜๊ฒŒ ๋งŽ์•„์ง€๋ฉด ์ด ๋ฐฉ๋ฒ•์„ ์“ฐ๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ, ์†๋„ ๋ฉด์—์„œ ๋” ์œ ๋ฆฌํ•˜๋‹ค๊ณ  ํ•œ๋‹ค. 

codes2 = [ord(s) for i in chasrs]

๋ฆฌ์ŠคํŠธ ์•ˆ์—์„œ ๋ฐ”๋กœ for๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

 

*** Comprehending LIst + Map, Filter

์ด ๋ฐฉ๋ฒ• ์—ญ์‹œ ์†๋„๊ฐ€ ์•ฝ๊ฐ„ ์šฐ์„ธํ•จ . 

codes3 = [ord(s) for s in chars if ord(s) > 40]
codes4 = list(filter(lambda x : x > 40, map(ord, chars)))

๊ฒฐ๊ณผ์ ์œผ๋กœ codes3์™€ codes4 ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ๊ฐ’๋“ค์„ ๊ฐ™๋‹ค. codes3๋Š” for๋ฌธ๊ณผ if๋ฌธ์„ ์‚ฌ์šฉ , codes4๋Š” mapํ•จ์ˆ˜๋กœ chars์˜ ๊ฐ’๋“ค์— ordํ•จ์ˆ˜๋ฅผ ์ ์šฉ ์‹œํ‚จ ํ›„ , lambda, filter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 40๋ณด๋‹ค ํฐ ๊ฐ’๋“ค๋งŒ ๋‚˜์˜ค๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

 

*** ์ถœ๋ ฅํ•ด๋ณด๊ธฐ

print('EX1-1 - ', codes1)
print('EX1-2 - ', codes2)
print('EX1-3 - ', codes3)
print('EX1-4 - ', codes4)
print('EX1-5 - ', [chr(s) for s in codes1])
print('EX1-6 - ', [chr(s) for s in codes2])
print('EX1-7 - ', [chr(s) for s in codes3])
print('EX1-8 - ', [chr(s) for s in codes4])

EX1-5๋ถ€ํ„ฐ๋Š” ord์™€ ๋ฐ˜๋Œ€๋กœ chr()๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค์‹œ ๋ฐ”๊พธ์–ด์„œ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด ์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค. 

 

 

 

*** ๋ฆฌ์ŠคํŠธ ์ฃผ์˜ํ•  ์  

marks1 = [['~'] * 3 for n in range(3)]
marks2 = [['~'] * 3] * 3

print("EX4-1 - ", marks1)
print("EX4-2 - ", marks2)

์ด ๋‘๊ฐ€์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด 

๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค. 

 

--> ๊ฐ’ ํ•˜๋‚˜ ๋ณ€๊ฒฝ 

marks1[0][1] = 'X'
marks2[0][1] = 'X'

print('EX4-3 - ', marks1)
print('EX4-3 - ', marks2)

๋ถ„๋ช… ํ˜•ํƒœ๋Š” ๊ฐ™์•˜๋Š”๋ฐ, 0๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ 1๋ฒˆ์งธ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ, ๋‘๋ฒˆ์งธ๋Š” 0, 1, 2๋ฒˆ์งธ์˜ 1๋ฒˆ์จฐ ๊ฐ’์ด ๋ชจ๋‘ ๋ฐ”๋€Œ์—ˆ๋‹ค. 

 

---> ์ฆ๋ช… 

print('EX4-5 - ', [id(i) for i in marks1])
print('EX4-6 - ', [id(i) for i in marks2])

์œ„์™€ ๊ฐ™์ด marks1์˜ ๋ฆฌ์ŠคํŠธ ๋“ค์€ ๋ชจ๋‘ ๋‹ค๋ฅธ ๊ฐ์ฒด์ด๋ฏ€๋กœ id๊ฐ’์ด ๋‹ค๋ฅด์ง€๋งŒ, marks2์˜ ๋ฆฌ์ŠคํŠธ๋“ค์€ ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ๋ฅผ 3๋ฒˆ ๋ฐ˜๋ณตํ•ด ๋†“์€ ๊ฒƒ๊ณผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— id๊ฐ’์ด ๋ชจ๋‘ ๊ฐ™๋‹ค. ๊ฐ™์€ ๊ณณ์„ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 0๋ฒˆ์จฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ’๋งŒ ๋ฐ”๊พธ๋ฉด 1, 2๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ’๋„ ํ•จ๊ป˜ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด๋‹ค. 

 

๋ฐ์ดํ„ฐ ๋ถ„์„์„ ํ•  ๋•Œ ์ด๋Ÿฐ ์‹ค์ˆ˜๋ฅผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋”์ฐํ•˜๋‹ค!

 

 

 

- ์ œ๋„ˆ๋ ˆ์ดํ„ฐ , Generator & Array 

(1) ์ œ๋„ˆ๋ ˆ์ดํ„ฐ

์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๊ฐœ์˜ ํ•ญ๋ชฉ์„ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์œ ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋น…๋ฐ์ดํ„ฐ๋ฅผ ์“ธ ๋•Œ๋Š” Generator๊ฐ€ ํ›จ์”ฌ ์šฐ์„ธํ•˜๋‹ค๊ณ  ํ•œ๋‹ค. 

tuple_g = (ord(s) for s in chars)  # ์•„์ง ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ ์ƒํƒœ๊ฐ€ ์•„๋‹˜

 

์ด์ฒ˜๋Ÿผ ํŠœํ”Œ ์•ˆ์— for๋ฌธ์„ ์จ์ฃผ๋ฉด ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ์•„์ง ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ! 

 

(2) Array 

import array 
array_g = array.array('I', (ord(s) for s in chars))

 

array๋ฅผ importํ•ด์ค€๋‹ค. ์ฒ˜์Œ ์จ๋ณด์•„์„œ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„์„œ ์ฝ์–ด๋ณด์•˜๋‹ค. 

<array.array ์ฐธ๊ณ  ๋ฌธ์„œ>

https://docs.python.org/3/library/array.html

 

array — Efficient arrays of numeric values — Python 3.9.6 documentation

array — Efficient arrays of numeric values This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers. Arrays are sequence types and behave very much like lists, except that the

docs.python.org

 

*** ์ถœ๋ ฅ, ์‚ฌ์šฉํ•ด๋ณด๊ธฐ 

print('EX2-1 - ', tuple_g)  
print('EX2-2 - ', next(tuple_g))
print('EX2-3 - ', next(tuple_g))
print('EX3-4 - ', array_g)
print('EX3-4 - ', array_g.tolist())

์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” next ๋˜๋Š” for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. 

array๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด tolist()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

 

 

*** ์ œ๋„ˆ๋ ˆ์ดํ„ฐ for๋ฌธ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ 

print('EX3-1 - ', ('%s' % c + str(n) for c in ['A', 'B', 'C', 'D'] for n in range(1, 11)))

for s in ('%s' % c + str(n) for c in ['A', 'B', 'C', 'D'] for n in range(1, 11)):
    print('EX3-2 - ', s)

for๋ฌธ์œผ๋กœ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด์—์„œ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์ฃผ๋Š” ์ฝ”๋“œ  . 

 

 

 

- Tuple Advanced

*** packing & unpacking

print('EX5-1 - ', divmod(100, 9))
print('EX5-2 - ', divmod(*(100, 9)))
print('EX5-3 - ', *(divmod(100, 9)))

divmod๋Š” ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ํŠœํ”Œ ์•ž์— *์„ ๋ถ™์ด๋ฉด unpaking์ด ๋œ๋‹ค๋Š” ๊ฒƒ !

 

x, y, *rest = range(10)
print('EX5-4 - ', x, y, rest)
x, y, *rest = range(2)
print('EX5-5 - ', x, y, rest)
x, y, *rest = 1, 2, 3, 4, 5
print('EX5-6 - ', x, y, rest)

 

์ฐธ๊ณ ) ** ---> dictionary ๋ฅผ ๋ฐ›์Œ 

(ex) def test(*args, **args):

 

 

- Mutable (๊ฐ€๋ณ€) vs Immutable (๋ถˆ๋ณ€) 

l = (10, 15, 20)
m = [10, 15, 20]

print('EX6-1 - ', l, m, id(l), id(m))

๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ ๊ฐ์ฒด๋ฅผ ๊ฐ๊ฐ ์ƒ์„ฑํ•ด์ค€๋‹ค. 

# ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑ ---> ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ์žก์•„๋จน์„ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ 
l = l * 2
m = m * 2 

print('EX6-2 - ', l, m, id(l), id(m))

# ์ž์ฒด์—์„œ ์—ฐ์‚ฐ 
l *= 2 
m *= 2

print('EX6-3 - ', l, m, id(l), id(m))

l = l*2 ์™€ ๊ฐ™์€ ์ฝ”๋“œ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ถœ๋ ฅ๋œ id๊ฐ’์„ ๋ด๋„ ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ ๋ชจ๋‘ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

 

l *= 2 ์™€ ๊ฐ™์ด ์ž์ฒด์—์„œ ์—ฐ์‚ฐ์„ ํ•  ๋•Œ๋Š” ๊ฐ€๋ณ€ํ˜•์ธ ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ , ๋ถˆ๋ณ€ํ˜•์ธ ํŠœํ”Œ์€ ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค. 

 

 

-  sort vs sorted 

f_list = ['ornage', 'apple', 'mango', 'papaya', 'lemon', 'strawberry','coconut']

 

(1) sorted : ์ •๋ ฌ ํ›„ ์ƒˆ๋กœ์šด ๊ฐ์ฒด ๋ฐ˜ํ™˜

print('EX7-1 - ', sorted(f_list))
print('EX7-2 - ', sorted(f_list, reverse=True)) # ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ 
print('EX7-3 - ', sorted(f_list, key=len)) # ๊ธธ์ด์ˆœ์„œ 
print('EX7-4 - ', sorted(f_list, key=lambda x: x[-1]))
print('EX7-5 - ', sorted(f_list, key=lambda x: x[-1], reverse=True))

print('EX7-6 - ', f_list)

sorted ์˜ ์ธ์ž์—๋Š” ์ •๋ ฌํ•  ๋ฆฌ์ŠคํŠธ์™€ ์ •๋ ฌํ•  ์กฐ๊ฑด์„ ๋„ฃ์–ด์ค€๋‹ค. 

๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์›๋ณธ ๋ฆฌ์ŠคํŠธ์—๋Š” ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค. 

 

 

(2) sort : ์ •๋ ฌ ํ›„ ๊ฐ์ฒด ์ง์ ‘ ๋ณ€๊ฒฝ 

a = f_list.sort()

print('EX7-7 - ', f_list.sort(), f_list)
print('EX7-8 - ', f_list.sort(reverse=True), f_list)
print('EX7-9 - ', f_list.sort(key=len), f_list)
print('EX7-10 - ', f_list.sort(key=lambda x: x[-1]), f_list)
print('EX7-11 - ', f_list.sort(key=lambda x: x[-1], reverse=True), f_list)

sort๋Š” ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋•Œ๋ฌธ์—, f_list.sort() ์ž์ฒด๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด None์ด ๋‚˜์˜จ๋‹ค. (None---> ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋‹ค๋Š” ๋œป) ๊ทธ๋ž˜์„œ ์›๋ณธ์ธ f_list๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผ ๋ณ€๊ฒฝ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/21

 

[python ์‹ฌํ™”] 4. ๋ฐ์ดํ„ฐ ๋ชจ๋ธ(Data Model), Nametuple

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/20 https://silvercoding.tistory.com/19 https://silvercoding.tistory.com/18 [python ์‹ฌํ™”] 1. ๊ฐ์ฒด์ง€ํ–ฅ(OOP), ํด..

silvercoding.tistory.com

 

 

<๋งค์ง ๋ฉ”์†Œ๋“œ ๊ด€๋ จ ๋ฌธ์„œ>

https://docs.python.org/3/reference/datamodel.html#special-method-names

 

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

https://www.tutorialsteacher.com/python/magic-methods-in-python

 

Magic or Dunder Methods in Python

Python - Magic or Dunder Methods Magic methods in Python are the special methods that start and end with the double underscores. They are also called dunder methods. Magic methods are not meant to be invoked directly by you, but the invocation happens inte

www.tutorialsteacher.com

ํŒŒ์ด์ฌ์˜ ์ค‘์š”ํ•œ ํ•ต์‹ฌ ํ”„๋ ˆ์ž„ ์›Œํฌ : ์‹œํ€€์Šค(Sequence), ๋ฐ˜๋ณต(Iterator), ํ•จ์ˆ˜(Functions), ํด๋ž˜์Šค(Class

 

 

 

- ๋งค์ง๋ฉ”์†Œ๋“œ 

n = 100 
print(dir(n)) # ๋ชจ๋“  ์†์„ฑ ๋ฐ ๋ฉ”์†Œ๋“œ ์ถœ๋ ฅ 

__xxx__ ----> ๋งค์ง ๋ฉ”์†Œ๋“œ 

 

 

*** ์‚ฌ์šฉ 

print('EX1-1 - ', n + 200)
print('EX1-2 - ', n.__add__(200))
print('EX1-3 - ', n.__doc__)
print('EX1-4 - ', n.__bool__(), bool(n))
print('EX1-5 - ', n * 100, n.__mul__(100))

๋งค์ง ๋ฉ”์†Œ๋“œ๋Š” ํŒŒ์ด์ฌ์—์„œ ์ด๋ฏธ ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. ์ด๋ณด๋‹ค๋„ ๋” ๋‹ค์–‘ํ•œ ๋งค์ง ๋ฉ”์†Œ๋“œ๋“ค์ด ์žˆ๋‹ค. 

์ด๋Ÿฌํ•œ ์ •์ˆ˜ํ˜• ํƒ€์ž…์˜ ๊ฐ์ฒด๋Š” ๊ทธ๋ƒฅ + , * ์ด๋Ÿฌํ•œ ๊ธฐํ˜ธ๋“ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ์ง์ ‘ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ง์ ‘ ์ •์˜๋ฅผ ํ•ด์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

- ํด๋ž˜์Šค ๋งค์ง ๋ฉ”์†Œ๋“œ ์˜ˆ์ œ 

(1) ํด๋ž˜์Šค ์ƒ์„ฑ 

class Student:
    # ๋งค์ง ๋ฉ”์„œ๋“œ์ž„. ํŒŒ์ด์ฌ์—์„œ ์ด๋ฏธ ์ •์˜๋œ ๋ฉ˜์„œ๋“œ 
    def __init__(self, name, height):
        self._name = name
        self._height = height

    def __str__(self):
        return 'Student Class Info : {}, {}'.format(self._name, self._height)

    def __ge__(self, x):
        print("Called >> __ge__ Method.")
        if self._height >= x._height:
            return True
        else:
            return False

    def __le__(self, x):
        print("Called >> __le__ Method")
        if self._height <= x._height:
            return True
        else:
            return False

    def __sub__(self, x):
        print('Called >> __sub__ Method.')
        return self._height - x._height

 

์œ„์˜ ๋ฌธ์„œ์—์„œ ๊ฐ€์ ธ์˜จ ๋งค์ง ๋ฉ”์„œ๋“œ ์˜๋ฏธ 

__str__ ---> To get called by built-int str() method to return a string representation of a type.

__le__ ---> To get called on comparison using <= operator.

__ge__ ---> To get called on comparison using >= operator.

__sub__ ---> To get called on subtraction operation using - operator.

 

 

(2) ์‚ฌ์šฉ 

*** ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ 

s1 = Student('James', 181)
s2 = Student('Mie', 165)

Student ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ!

 

*** ๋งค์ง ๋ฉ”์†Œ๋“œ ์ถœ๋ ฅ 

print('EX2-1 - ', s1 >= s2)
print('EX2-2 - ', s1 <= s2)
print('EX2-3 - ', s1 - s2)
print('EX2-4 - ', s2 - s1)
print('EX2-5 - ', s1)
print('EX2-6 - ', s2)

ํด๋ž˜์Šค์— ๋งค์ง๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ฐ์ฒด์™€ ๊ฐ์ฒด๊ฐ„์˜ ๋น„๊ต๋กœ ๋ฐ›์•„๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค. 

ํด๋ž˜์Šค์—์„œ ์ง์ ‘ ์ •์˜๋ฅผ ํ•ด๋†จ์œผ๋ฏ€๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š๊ณ  ์ •์˜ํ•œ ๋Œ€๋กœ ๋‚˜์˜จ๋‹ค. 

์ด์™€๊ฐ™์ด ๋งค์ง๋ฉ”์†Œ๋“œ๋ฅผ ์ง์ ‘ ์ •์˜ํ•˜์—ฌ ์œ ์šฉํ•œ ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

- ํด๋ž˜์Šค ๋ฉ”์ง๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ์˜ˆ์ œ 2

(1) ํด๋ž˜์Šค ์ƒ์„ฑ 

class Vector(object):
    def __init__(self, *args):
        '''Create a vector, example : v = Vector(1, 2)'''
        if len(args) == 0:
            self._x, self._y = 0, 0
        else:
            self._x, self._y = args

    def __repr__(self):
        '''Returns the Vector information'''
        return 'Vector(%r, %r)' % (self._x, self._y)

    def __add__(self, other):
        '''Returns the vector addition of self and other'''
        return Vector(self._x + other._x, self._y + other._y)

    def __mul__(self, t):
        '''Returns the vector addition of self and other'''
        return Vector(self._x * t, self._y * t)

    def __bool__(self):
        return bool(max(self._x, self._y))

๋ฒกํ„ฐ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ํด๋ž˜์Šค์ด๋‹ค. 

__add__๋ฉ”์†Œ๋“œ๋Š” ๋ฒกํ„ฐ๋ผ๋ฆฌ ๊ฐ ์ขŒํ‘œ์— ๋Œ€ํ•œ ๋ง์…ˆ, __mul__๋ฉ”์†Œ๋“œ๋Š” ์‹ค์ˆ˜๋ฅผ ์ธ์ž์— ๋„ฃ์–ด์ฃผ๋ฉด ๊ฐ ์ขŒํ‘œ์— ๊ณฑํ•ด์ง€๋Š” ๊ฒƒ์„ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด๋‹ค. __bool__๋ฉ”์†Œ๋“œ๋Š” x, y ์ขŒํ‘œ ์ค‘ ํฐ ๊ฐ’์ด 0์ด ์•„๋‹ˆ๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค. (์ •๋ง ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์€ ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค!)

 

 

(2) ์‚ฌ์šฉ 

# Vector ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
v1 = Vector(3, 5)
v2 = Vector(15, 20)
v3 = Vector()

# ๋งค์ง ๋ฉ”์†Œ๋“œ ์ถœ๋ ฅ 
print("EX3-1 - ", Vector.__init__.__doc__)
print("EX3-2 - ", Vector.__repr__.__doc__)
print("EX3-3 - ", Vector.__add__.__doc__)
print("EX3-4 - ", v1, v2, v3)
print("EX3-5 - ", v1 + v2)
print("EX3-6 - ", v1 * 4)
print("EX3-7 - ", v2 * 10)
print('EX3-8 - ', bool(v1), bool(v2))
print('EX3-9 - ', bool(v3))

__doc__ ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ์ฃผ์„ ์„ค๋ช…์ด ์ถœ๋ ฅ๋œ๋‹ค. ๋‚˜๋จธ์ง€๋„ ์ž˜ ์‹คํ–‰๋˜๋ฉด ์„ฑ๊ณต!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

<์ด์ „ ๊ธ€>

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)

๋ฆฌ์ŠคํŠธ์— ํ•œ๋ฒˆ์— ๋•Œ๋ ค๋ฐ•์•„ ์ฃผ๋Š” ๋ฒ•์ด๋‹ค. ๋ญ๋“ ์ง€ ๊ณผํ•˜๋ฉด ๋…์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€์ง€๋งŒ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋‘๊ธฐ. 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/19

 

[python ์‹ฌํ™”] 2. ํด๋ž˜์Šค ๊ธฐ์ดˆ, ํด๋ž˜์Šค ๋ณ€์ˆ˜ , ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/18 [python ์‹ฌํ™”] 1. ๊ฐ์ฒด์ง€ํ–ฅ(OOP), ํด๋ž˜์Šค ๊ธฐ์ดˆ ์‚ฌ์šฉ ์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  1. ๊ฐ์ฒด์ง€ํ–ฅ vs ์ ˆ์ฐจ์ง€ํ–ฅ ๊ฐ„๋‹จํ•œ..

silvercoding.tistory.com

 

 

- ํด๋ž˜์Šค ํ™•์žฅ 

์ด๋ฒˆ์—๋Š” ํด๋ž˜์Šค์— ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ, ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ , ์Šคํ…Œ์ดํ‹ฑ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„์„ ํ•ด ๋ณธ๋‹ค. 

class Student(object):
    """
    Student Class
    Author : Kim
    Date : 2021. 6. 28 
    Description : Class, Static, Instance Method
    """

    # Class Variable
    tuition_per = 1.0

    def __init__(self, id, first_name, last_name, email, grade, tuition, gpa):
        self._id = id
        self._first_name = first_name
        self._last_name = last_name
        self._email = email
        self._grade = grade
        self._tuition = tuition
        self._gpa = gpa

    # Instance Method 
    def full_name(self):
        return "{}, {}".format(self._first_name, self._last_name)
    
    # Instance Method
    def detail_info(self):
        return 'Student Detail Info : {}, {}, {}, {}, {}, {}'.format(self._id, self.full_name(), self._email, self._grade, self._tuition, self._gpa)

    # Instance Method
    def get_fee(self):
        return 'Beforoe Tuition -> Id : {}, fee : {}'.format(self._id, self._tuition)

    # Instance Method
    def get_fee_culc(self):
        return 'After tuition -> Id : {}, fee : {}'.format(self._id, self._tuition * Student.tuition_per) 
    
    # Instance Method
    def __str__(self):
        return 'Student Info -> name : {} grade : {} email : {}'.format(self.full_name(), self._grade, self._email)
        
    # Class Method
    @classmethod
    def raise_fee(cls, per):
        if per <= 1:
            print('Please Enter 1 or More')
            return 
        Student.tuition_per = per
        print('Succeed! tuition increased.')

    # Class Method
    @classmethod
    def student_const(cls, id, first_name, last_name, email, grade, tuition, gpa):
        return cls(id, first_name, last_name, email, grade, tuition * cls.tuition_per, gpa)

    # Static Method
    @staticmethod # ---> ์š”์ƒˆ๋Š” ์ž˜ ์“ฐ์ง„ ์•Š๋Š”๋‹ค. 
    def is_scolarship_st(inst):
        if inst._gpa >= 4.3:
            return '{} is a scholarship recipient'.format(inst._last_name)
        return 'Sorry, Not a scholarship recipient'

Instance Method๋Š” self์˜ ์ ‘๊ทผ์ž(๊ฐœ๊ฐœ์ธ, ๊ณ ์œ ํ•œ ๊ฐ์ฒด)๋ฅผ ์ด์šฉํ•˜์—ฌ ์†์„ฑ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฒซ๋ฒˆ์งธ ์ธ์ž์— self๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์€ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•˜๋ฉด ๋œ๋‹ค. 

 

Class Method ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ํด๋ž˜์Šค๋ฅผ ๋ฐ›์•„์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„ cls๋ผ๊ณ  ์“ด๋‹ค. ๋˜ , ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์ „์— ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. (@classmethod) ํด๋ž˜์Šค ์ž์ฒด๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

Static Method ์ผ๋ฐ˜ ํ•จ์ˆ˜์˜ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ์ฆ‰, ํด๋ž˜์Šค ์•ˆ์— ๊ตณ์ด ๋„ฃ์ง€ ์•Š์•„๋„ ๋˜๊ณ  ํด๋ž˜์Šค ๋ฐ–์—์„œ ์ƒ์„ฑํ•ด๋„ ๋˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ๊ทธ๋ž˜๋„ ์œ„์—์ฒ˜๋Ÿผ ์žฅํ•™๊ธˆ ๋ฐ›๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋Š” studentํด๋ž˜์Šค์™€ ์—ฐ๊ด€์ด ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ์•ˆ์— ์จ์ฃผ๋Š” ๊ฒƒ์ด ๋” ๋ณด๊ธฐ ํŽธํ•  ๊ฒƒ์ด๋‹ค. 

 

 

 

- ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ 

student_1 = Student(1, 'Kim', "silver", 'student1@naver.com', '1', 400, 3.5)
student_2 = Student(2, 'Han', "golye", 'student2@daum.net', '2', 500, 4.3)
# ๊ธฐ๋ณธ์ •๋ณด 
print(student_1)
print(student_2)

print()

# ์ „์ฒด ์ •๋ณด
print(student_1.detail_info())
print(student_2.detail_info())

 

 

- ๋“ฑ๋ก๊ธˆ 

# ํ•™๋น„ ์ •๋ณด (๋“ฑ๋ก๊ธˆ ์ธ์ƒ ์ „)
print(student_1.get_fee())
print(student_2.get_fee())

* ํ•™๋น„ ์ธ์ƒ (ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ)

# ํ•™๋น„ ์ธ์ƒ (ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ ๋ฏธ์‚ฌ์šฉ) ---> ์ด๋ ‡๊ฒŒ ์ง์ ‘ ์ ‘๊ทผํ•˜๋Š” ๊ฑฐ ์ข‹์ง€ ์•Š๋‹ค. 
# Student.tuition_per = 1.2

# ํ•™๋น„ ์ธ์ƒ (ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ) 
Student.raise_fee(1.2)

* ์ธ์ƒ ์‹œํ‚จ ํ›„์˜ ํ•™๋น„ ์ •๋ณด 

print(student_1.get_fee_culc())
print(student_2.get_fee_culc())

 

 

- ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜๊ธฐ 

student_3 = Student.student_const(3, 'Park', 'silver', 'Student3@gmail.com', '3', 550, 4.5)
student_4 = Student.student_const(4, 'Cho', 'golye', 'student4@gmail.com', '4', 600, 4.1)

Class Method๋กœ ์ƒ์„ฑํ•ด ๋†“์•˜๋˜ student_const๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.  

 

*** ๋˜‘๊ฐ™์ด ์‹ค์Šต 

# ์ „์ฒด ์ •๋ณด 
print(student_3.detail_info())
print(student_4.detail_info())
print()

# ํ•™์ƒ ํ•™๋น„ ๋ณ€๊ฒฝ ํ™•์ธ
print(student_3._tuition)
print(student_4._tuition)
print()

 

 

- Static Method ๋ฅผ ์ด์šฉํ•˜์—ฌ ์žฅํ•™๊ธˆ ํ˜œํƒ ์—ฌ๋ถ€ ์•Œ์•„๋ณด๊ธฐ 

(1) static method ๋ฏธ์‚ฌ์šฉ 

def is_scholarship(inst):
    if inst._gpa >= 4.3:
        return '{} is a scholarship recipient'.format(inst._last_name)
    return 'Sorry, Not a scholarship recipient'

์ด์ฒ˜๋Ÿผ ํด๋ž˜์Šค ์•ˆ์— ๊ตณ์ด ๋„ฃ์ง€ ์•Š์•„๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

* ์‚ฌ์šฉ

print(is_scholarship(student_1))
print(is_scholarship(student_2))
print(is_scholarship(student_3))
print(is_scholarship(student_4))

์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜์— ์ธ์Šคํ„ด์Šค๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ์žฅํ•™๊ธˆ ๋Œ€์ƒ ์—ฌ๋ถ€๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. 

 

(2) static method ์‚ฌ์šฉ 

Student ํด๋ž˜์Šค ์•ˆ์— ๊ตฌํ˜„ํ•ด ๋†“์€ static method์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 

print(Student.is_scolarship_st(student_1))
print(Student.is_scolarship_st(student_2))
print(Student.is_scolarship_st(student_3))
print(Student.is_scolarship_st(student_4))


print(student_1.is_scolarship_st(student_1))
print(student_2.is_scolarship_st(student_2))
print(student_3.is_scolarship_st(student_3))
print(student_4.is_scolarship_st(student_4))

Student ํด๋ž˜์Šค ์ž์ฒด๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ณ , ๊ฐ์ฒด๋ฅผ ํ†ตํ•œ ์ ‘๊ทผ ๋˜ํ•œ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/18

 

[python ์‹ฌํ™”] 1. ๊ฐ์ฒด์ง€ํ–ฅ(OOP), ํด๋ž˜์Šค ๊ธฐ์ดˆ ์‚ฌ์šฉ

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  1. ๊ฐ์ฒด์ง€ํ–ฅ vs ์ ˆ์ฐจ์ง€ํ–ฅ ๊ฐ„๋‹จํ•œ ๋น„๊ต ํŒŒ์ด์ฌ์€ OOP (๊ฐ์ฒด์ง€ํ–ฅ) ์–ธ์–ด์ด๋‹ค.  ๊ฐ์ฒด์ง€ํ–ฅ๊ณผ ์ ˆ์ฐจ์ง€ํ–ฅ์€ ๋•Œ์— ๋”ฐ๋ผ ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ๋‹ค. ๊ฐ์ฒด์ง€ํ–ฅ์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ์žฌ

silvercoding.tistory.com

 

 

 

ํด๋ž˜์Šค ํ™•์žฅ 

Student ํด๋ž˜์Šค์— ํด๋ž˜์Šค ๋ณ€์ˆ˜์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ด์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ดํ•ด ํ•ด๋ณด์ž.

- ํด๋ž˜์Šค ์ƒ์„ฑ 

class Student():
    """
    Student Class
    Author : silver
    Date : 2021. 06. 30 
    """

    # ํด๋ž˜์Šค ๋ณ€์ˆ˜ 
    student_count = 0 

    def __init__(self, name, number, grade, details, email=None):
        # ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜
        self._name = name
        self._number = number
        self._grade = grade
        self._details = details
        self._email = email

        Student.student_count += 1 

    def __str__(self):
        return 'str {}'.format(self._name)

    def __repr__(self):
        return 'repr {}'.format(self._name)

    def detail_info(self):
        print('Current Id : {}'.format(id(self)))
        print('Student Detail Info : {} {} {}'.format(self._name, self._email, self._details))

    def __del__(self):
        Student.student_count -= 1 

์šฐ์„  ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. ์ฃผ์„์œผ๋กœ ํด๋ž˜์Šค ๋ณ€์ˆ˜์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•ด ๋†“์•˜๋‹ค. 

๋ง๊ทธ๋Œ€๋กœ ํด๋ž˜์Šค ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ์ž์ฒด์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜, ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜์ด๋‹ค. ๋”ฐ๋ผ์„œ ํด๋ž˜์Šค ์•ˆ์— student_count ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ , ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ์—์„œ ์“ด ๊ฒƒ๊ณผ ๊ฐ™์ด Student.student_count += 1 ์ฒ˜๋Ÿผ ํด๋ž˜์Šค๋กœ ์ ‘๊ทผํ•˜์—ฌ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” self๊ฐ€ ๋ถ™์–ด ์žˆ๋‹ค. ๊ฐ์ฒด๋ฅผ ํ†ตํ•˜์—ฌ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

 

 

- ๊ฐ์ฒด ์ƒ์„ฑ 

studt1 = Student('Cho', 2, 3, {'gender': 'Male', 'score1': 65, 'score2': 44})
studt2 = Student('chang', 4, 1, {'gender': 'Female', 'score1': 85, 'score2': 74}, "onestein@google.com")

 ๋‘ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. 

 

 

- id ํ™•์ธ 

print(id(studt1))
print(id(studt2))

 

 

*** == , is ์˜ ์ฐจ์ด 

== : ๊ฐ’์„ ๋น„๊ต 

print(studt1._name == studt2._name)

is : id ๊ฐ’์„ ๋น„๊ต 

print(studt1 is studt2)

 

 

- dir & __dict__ & __doc__

(1) ๋‚ด์žฅํ•จ์ˆ˜ dir 

https://wikidocs.net/10307

 

์œ„ํ‚ค๋…์Šค

์˜จ๋ผ์ธ ์ฑ…์„ ์ œ์ž‘ ๊ณต์œ ํ•˜๋Š” ํ”Œ๋žซํผ ์„œ๋น„์Šค

wikidocs.net

dir() ๋‚ด์žฅ ํ•จ์ˆ˜๋Š” ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ(method)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๋‚˜์—ดํ•ด ์ค€๋‹ค. 

print(dir(studt1))
print(dir(studt2))

๋”ฐ๋ผ์„œ Student์˜ ๊ฐ์ฒด studt1, studt2๋ฅผ dir ์ธ์ž์— ๋„ฃ์–ด์„œ ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ํด๋ž˜์Šค๋ณ€์ˆ˜๊นŒ์ง€ ๋ชจ๋‘ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

(2) __dict__ 

https://wikidocs.net/1743

 

์œ„ํ‚ค๋…์Šค

์˜จ๋ผ์ธ ์ฑ…์„ ์ œ์ž‘ ๊ณต์œ ํ•˜๋Š” ํ”Œ๋žซํผ ์„œ๋น„์Šค

wikidocs.net

ํด๋ž˜์Šค ๋˜๋Š” ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ํ™•์ธํ•œ๋‹ค. 

print(studt1.__dict__)
print(studt2.__dict__)

์ด๋ ‡๊ฒŒ dictionary ๊ตฌ์กฐ๋กœ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. 

 

 

 

(3) __doc__ 

print(Student.__doc__)

__doc__ ํด๋ž˜์Šค ์•ˆ์—์žˆ๋Š” ์„ค๋ช… ์ฃผ์„์„ ๋ณด์—ฌ์ค€๋‹ค. 

 

 

 

- ๋ฉ”์†Œ๋“œ ์‹คํ–‰ํ•ด๋ณด๊ธฐ 

# ์ธ์Šคํ„ด์Šค๋กœ ์ ‘๊ทผ 
studt1.detail_info()
studt2.detail_info()


# Student.detail_info() ---> ์—๋Ÿฌ ๋‚จ 


# ํด๋ž˜์Šค๋กœ ์ ‘๊ทผ
Student.detail_info(studt1)
Student.detail_info(studt2)

์ธ์Šคํ„ด์Šค๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ด๊ณ , ํด๋ž˜์Šค๋กœ๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ํด๋ž˜์Šค๋กœ ์ ‘๊ทผํ•˜๋ฉด ์ธ์ž์— ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. self๋ฅผ ์ฑ„์›Œ์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

 

 

 

- __class__ 

print(studt1.__class__, studt2.__class__)
print(id(studt1.__class__) == id(studt2.__class__))

Student ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ฐ์ฒด๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ id๊ฐ’์„ ๊ฐ€์ง€์ง€๋งŒ, ํด๋ž˜์Šค๋Š” ๊ฐ™์€ id๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. (๋‹น์—ฐํ•œ ์†Œ๋ฆฌ) 

 

 

 

- ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ํด๋ž˜์Šค ๋ณ€์ˆ˜์˜ ์ ‘๊ทผ 

(1) ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ ‘๊ทผ

print(studt1._name, studt2._name)
print(studt1._email, studt2._email)

์ด๋ ‡๊ฒŒ ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•˜์—ฌ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. 

studt1._name = "Lee" ์ด๋Ÿฐ ์‹์œผ๋กœ ์ง์ ‘ ์ ‘๊ทผ์œผ๋กœ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ๋Š” ๋ฌธ๋ฒ•์ ์œผ๋กœ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค. 

 

 

 

(2) ํด๋ž˜์Šค ๋ณ€์ˆ˜ ์ ‘๊ทผ 

print(studt1.student_count)
print(studt2.student_count)
print(Student.student_count)

ํด๋ž˜์Šค ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค๋กœ๋„ , ํด๋ž˜์Šค ์ž์ฒด๋กœ๋„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ธ์Šคํ„ด์Šค๋กœ ์ ‘๊ทผ์„ ํ•  ๋•Œ์—๋Š” ์ธ์Šคํ„ด์Šค ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์—†์œผ๋ฉด ์ƒ์œ„(ํด๋ž˜์Šค) ์—์„œ ๊ฒ€์ƒ‰ํ•œ๋‹ค. ์ธ์Šคํ„ด์Šค ๊ฒ€์ƒ‰ -> ์ƒ์œ„ ๊ฒ€์ƒ‰ (ํด๋ž˜์Šค, ๋ถ€๋ชจ ํด๋ž˜์Šค) ์ˆœ์„œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ๋ณ€์ˆ˜ ์ƒ์„ฑ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

- __del__ ํ˜ธ์ถœ

del studt2

print(studt1.student_count)
print(Student.student_count)

__del__ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ student_count๋„ ์ค„์–ด๋“ค๋„๋ก ๋งŒ๋“ค์—ˆ์œผ๋ฏ€๋กœ ์ถœ๋ ฅํ•ด ๋ณด๋ฉด 1์ด ๋˜์–ด์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ 

 

 

1. ๊ฐ์ฒด์ง€ํ–ฅ vs ์ ˆ์ฐจ์ง€ํ–ฅ ๊ฐ„๋‹จํ•œ ๋น„๊ต

ํŒŒ์ด์ฌ์€ OOP (๊ฐ์ฒด์ง€ํ–ฅ) ์–ธ์–ด์ด๋‹ค.  

๊ฐ์ฒด์ง€ํ–ฅ๊ณผ ์ ˆ์ฐจ์ง€ํ–ฅ์€ ๋•Œ์— ๋”ฐ๋ผ ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ๋‹ค. 

๊ฐ์ฒด์ง€ํ–ฅ์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ๊ณผ ์ฝ”๋“œ ์ค‘๋ณต์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ ˆ์ฐจ์ง€ํ–ฅ์€ ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ์‹คํ–‰ํ•œ๋‹ค. ์‹คํ–‰์†๋„๊ฐ€ ๋น ๋ฅด์ง€๋งŒ ์œ ์ง€๋ณด์ˆ˜ , ๊ฐœ์„ ์‹œํ‚ค๋Š” ์ž‘์—…์ด ์–ด๋ ต๋‹ค. ๊ทธ๋ž˜์„œ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ, ๋””๋ฒ„๊น…๋„ ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. 

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

 

 

 

2.  ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด ํŒŒ์ด์ฌ - ํด๋ž˜์Šค ์‚ฌ์šฉํ•˜๊ธฐ 

๋งŒ์•ฝ ํ•™์ƒ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์งœ์•ผ ํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ๋ฒˆํ˜ธ, ํ•™๋…„์„ ํฌํ•จํ•œ๋‹ค๊ณ  ํ•˜๋ฉด , ํ•™์ƒ๋งˆ๋‹ค ๋”ฐ๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ๋กœ ์ง ๋‹ค๋ฉด ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๋„ ๋งŽ์•„์ง€๊ณ , ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์‰ฝ์ง€ ์•Š๋‹ค. ๋”ฐ๋ผ์„œ class๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑ์„ ํ•ด๋ณด๋„๋ก ํ•œ๋‹ค. 

class Student():
    def __init__(self, name, number, grade, details):
        self._name = name 
        self._number = number
        self._grade = grade
        self._details = details

    def __str__(self):  
        return 'str : {} - {}'.format(self._name, self._number)

    ### str ์ด ์—†์„ ๋–ˆ ์ด๊ฒƒ์ด ํ˜ธ์ถœ ๋จ 
    def __repr__(self):
        return 'repr : {} - {}'.format(self._name, self._number)

 Student ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” name, number, grade, details๋ฅผ ๊ธฐ์ž…ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

__str__ ์€ ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ํ˜ธ์ถœ์ด ๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. __repr__ ์€ ๋น„์Šทํ•œ๋ฐ , __str__์ด ์—†์œผ๋ฉด ์ด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. 

 

 

 

- ๊ฐ์ฒด ์ƒ์„ฑ 

student1 = Student('Kim', 1, 1, {'gender' : 'Male', 'score1' : 95, 'score2' : 88})
student2 = Student('Lee', 2, 2, {'gender' : 'Female', 'score1' : 77, 'score2' : 92})

ํ•™์ƒ ๋‘๋ช…์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. 

 

 

- ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋ณด๊ธฐ

print(student1.__dict__)
print(student2.__dict__)

 

 

- __str__, __repr__ ํ˜ธ์ถœ

students_list = []
students_list.append(student1)
students_list.append(student2)
students_list.append(student3)


for x in students_list:
    print(repr(x))
    print(x) # ์ง€์ •ํ•œ str์ด ๋‚˜์˜ด 

ํ•œ๋ฒˆ์— ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ฃผ๊ณ , for๋ฌธ์œผ๋กœ ์ถœ๋ ฅํ•ด์ค€๋‹ค. ๊ทธ๋ƒฅ ์ถœ๋ ฅ์„ ํ•˜๋ฉด str์ด ๋‚˜์˜ค๊ณ , repr์„ ํ˜ธ์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด repr(๊ฐ์ฒด)๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

 

 

 

 

 

 

+ Recent posts