๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(์์ด: Object-Oriented Programming, OOP) ์ผ๋ฐ์ ์ผ๋ก ํ๋ก๊ทธ๋จ์ ๋ง๋ค๋ ํญ์ ์ผ๋์ ๋ฌ์ผํ ์์ฃผ ์ค์ํ ํฌ์ธํธ 2๊ฐ์ง๊ฐ ์์ต๋๋ค. ๊ฐ์ ์ฝ๋๋ฅผ ๋ฐ๋ณตํ์ง ์๋๋ค. ์ฝ๋๋ ํญ์ ๋ฐ๋ ์ ์๋ค๋ ๊ฒ์ ๊ธฐ์ตํ๋ค. ์ด๋ฆ, ์๋์ง, ๋ฐ๋ฏธ์ง, ์ธ๋ฒคํ ๋ฆฌ๋ฅผ ๊ฐ์ง ๊ฐ๋จํ ์ผ๋ฆญํฐ๋ฅผ ๋ง๋ค์ด ๋ณด๋ ค๊ณ ํฉ๋๋ค
hero_name = '์์ด์ธ๋งจ' hero_health = 100 hero_damage = 200 hero_inventory = [ {'gold': 500}, {'weapon': '๋ ์ด์ '} ] ํ๋์ฉ ์ถ๊ฐํ๊ธฐ
hero_1_name = '์์ด์ธ๋งจ' hero_1_health = 100 hero_1_damage = 200 hero_1_inventory = [ {'gold': 500}, {'weapon': '๋ ์ด์ '} ]
hero_2_name = '๋ฐ๋ํ' hero_2_health = 300 hero_2_damage = 30 hero_2_inventory = [ {'gold': 300}, {'weapon': '์ฅ๊ฒ'} ]
hero_3_name = '์ธ๋ฒ๋ฆฐ' hero_3_health = 200 hero_3_damage = 50 hero_3_inventory = [ {'gold': 350}, {'weapon': 'ํด๋ก'} ]
monster_1_name = '๊ณ ๋ธ๋ฆฐ' monster_1_health = 90 monster_1_damage = 30 monster_1_inventory = [ {'gold': 50}, {'weapon': '์ฐฝ'} ]
monster_2_name = '๋๋๊ณค' monster_2_health = 200 monster_2_damage = 80 monster_2_inventory = [ {'gold': 200}, {'weapon': 'ํ์ผ'} ]
monster_3_name = '๋ฑํ์ด์ด' monster_3_health = 80 monster_3_damage = 120 monster_3_inventory = [ {'gold': 1000}, {'weapon': '์ต๋ฉด์ '} ] ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ์ฌ ์ฝ๋๋ฅผ ๋ฐ๊ฟ๋ด ๋๋ค. hero_name = ['์์ด์ธ๋งจ', '๋ฐ๋ํ', '์ธ๋ฒ๋ฆฐ'] hero_health = [100, 300, 200] hero_damage = [200, 30, 50] hero_inventory = [ {'gold': 500,'weapon': '๋ ์ด์ '}, {'gold': 300, 'weapon': '์ฅ๊ฒ'}, {'gold': 350, 'weapon': 'ํด๋ก'} ] monster_name = ['๊ณ ๋ธ๋ฆฐ', '๋๋๊ณค', '๋ฑํ์ด์ด'] monster_health = [90, 200, 80] monster_damage = [30, 80, 120] monster_inventory = [ {'gold': 50,'weapon': '์ฐฝ'}, {'gold': 200, 'weapon': 'ํ์ผ'}, {'gold': 1000, 'weapon': '์ต๋ฉด์ '} ] ์ค๋ฅ๊ฐ ๋ ๊ฐ๋ฅ์ฑ๋ค์ ์ดํด๋ณด๊ฒ ๋๋ค๋ฉด import json hero_name = ['์์ด์ธ๋งจ', '๋ฐ๋ํ', '์ธ๋ฒ๋ฆฐ'] hero_health = [100, 300, 200] hero_damage = [200, 30, 50] hero_inventory = [ {'gold': 500,'weapon': '๋ ์ด์ '}, {'gold': 300, 'weapon': '์ฅ๊ฒ'}, {'gold': 350, 'weapon': 'ํด๋ก'} ] monster_name = ['๊ณ ๋ธ๋ฆฐ', '๋๋๊ณค', '๋ฑํ์ด์ด'] monster_health = [90, 200, 80] monster_damage = [30, 80, 120] monster_inventory = [ {'gold': 50,'weapon': '์ฐฝ'}, {'gold': 200, 'weapon': 'ํ์ผ'}, {'gold': 1000, 'weapon': '์ต๋ฉด์ '} ]
def hero_dies(hero_index): del hero_name[hero_index] del hero_health[hero_index] del hero_damage[hero_index] # <--- ์ค์๋ก del hero_inventory[hero_index]ํ์ง ์์๋ค๋ฉด
hero_dies(0) print hero_name[0] print hero_health[0] print hero_damage[0] print json.dumps(hero_inventory[0], ensure_ascii=False) ์ ์ฅํ ์คํ์
๋ฐ๋ํ 300 30 {"weapon": "๋ ์ด์ ", "gold": 500} ์์ ์ฝ๋์ ๊ฐ์ด ํ์ด๋ก์ ์๋์ง๊ฐ 0์ด ๋์ด ์ฃฝ์์๋ ํ์ด๋ก๋ฅผ ๋ฆฌ์คํธ์์ ์ง์ฐ๋ ํจ์๋ฅผ ์ถ๊ฐํ์ต๋๋ค. ๊ทธ๋ฐ๋ฐ ๊ฐ๋ฐ์๊ฐ ์ค์๋ก ์ฝ๋ ํ์ค์ ๋ฃ์ง ์์๋ค๋ฉด "๋ฐ๋ํ"์ด ์ฃฝ์ "์์ด์ธ๋งจ"์ ๋ ์ด์ ๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ด๋ฌํ ๋ฌธ์ ๋ ๋ฐ์ ์ฝ๋์ ๊ฐ์ด ๊ฐ ํ์ด๋ก์ ๋ฐ์ดํฐ๋ฅผ ํ์ด์ฌ ์ฌ์ ์ ๋ฃ์ด ๋ฆฌ์คํธ๋ก ๋ฌถ์ด์ ํด๊ฒฐํ ์ ์์ต๋๋ค. import json heroes = [ {'name': '์์ด์ธ๋งจ', 'health': 100, 'damage': 200, 'inventory': {'gold': 500, 'weapon': '๋ ์ด์ '}}, {'name': '๋ฐ๋ํ', 'health': 300, 'damage': 30, 'inventory': {'gold': 300, 'weapon': '์ฅ๊ฒ'}}, {'name': '์ธ๋ฒ๋ฆฐ', 'health': 200, 'damage': 50, 'inventory': {'gold': 350, 'weapon': 'ํด๋ก'}} ] monsters = [ {'name': '๊ณ ๋ธ๋ฆฐ', 'health': 90, 'damage': 30, 'inventory': {'gold': 50, 'weapon': '์ฐฝ'}}, {'name': '๋๋๊ณค', 'health': 200, 'damage': 80, 'inventory': {'gold': 200, 'weapon': 'ํ์ผ'}}, {'name': '๋ฑํ์ด์ด', 'health': 80, 'damage': 120, 'inventory': {'gold': 1000, 'weapon': '์ต๋ฉด์ '}} ] print json.dumps(heroes, ensure_ascii=False) del heroes[0] print json.dumps(heroes, ensure_ascii=False) [{"inventory": {"weapon": "๋ ์ด์ ", "gold": 500}, "health": 100, "name": "์์ด์ธ๋งจ", "damage": 200}, {"inventory": {"weapon": "์ฅ๊ฒ", "gold": 300}, "health": 300, "name": "๋ฐ๋ํ", "damage": 30}, {"inventory": {"weapon": "ํด๋ก", "gold": 350}, "health": 200, "name": "์ธ๋ฒ๋ฆฐ", "damage": 50}] [{"inventory": {"weapon": "์ฅ๊ฒ", "gold": 300}, "health": 300, "name": "๋ฐ๋ํ", "damage": 30}, {"inventory": {"weapon": "ํด๋ก", "gold": 350}, "health": 200, "name": "์ธ๋ฒ๋ฆฐ", "damage": 50}] ๋ง์ฝ ํ์ด๋ก์ ์ธ๋ฒคํ ๋ฆฌ์ ์ฌ๋ฌ๊ฐ์ง ์์ดํ ์ ๊ฐ์ง ๊ฐ๋ฐฉ์ด ์๋ค๊ณ ํ์ ๋ ์ฌ์ ๊ณผ ๋ฆฌ์คํธ๋ ์ค์ฒฉ์ด ๋๊ณ ์ฝ๋๋ ๋์ฑ ๋ ๋ณต์กํด์ง ๊ฒ ์ ๋๋ค. OOP๋ฅผ ์ฌ์ฉํ ๋ ์ ๋๋ค.
์์์ ํด๋์ค๋ ์๋ก์ด ๋ฐ์ดํฐ ํ์ ์ ๋ง๋๋ ๋ธ๋ฃจํ๋ฆฐํธ๋ผ๊ณ ํ์ต๋๋ค. ๋ธ๋ฃจํ๋ฆฐํธ๋ ํ๊ตญ๋ง๋ก "์ฒญ์ฌ์ง"์ด๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ ๊ฑด์ถ๋ฌผ์ด๋ ์๋์ฐจ๋ฅผ ์ค๊ณํ ๋๋ฉด์ ๋๋ค. ์๋์ฐจ์ ๋ชจ๋ธ์ ๋์์ธํ๊ณ ์ค๊ณํ ๋ธ๋ฃจํ๋ฆฐํธ๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ ๋ชจ๋ธ์ ์๋์ฐจ๋ฅผ ์ํ๋ ๋งํผ ์ฐ์ด ๋ผ ์ ์๋๊ฑฐ์ฃ . ํ๋ก๊ทธ๋๋ฐ์ ํด๋์ค๋ ๊ฐ์ ๊ฐ๋ ์ ๋๋ค. ์์ ํ์ด๋ก์ ๋ชฌ์คํฐ ๋ํ ๊ฐ์ ์ข ๋ฅ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ ๋ค์์ ์ฝ๋์ ๊ฐ์ด ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ ๋ ผ๋ฆฌ์ ์ธ ์งํฉ์ผ๋ก ๋ฌถ์ ์๊ฐ ์์ต๋๋ค
class Character(object): def init(self, name, health, damage, inventory): self.name = name self.health = health self.damage = damage self.inventory = inventory
def __repr__(self):
return self.name
heroes = [] heroes.append(Character('์์ด์ธ๋งจ', 100, 200, {'gold': 500, 'weapon': '๋ ์ด์ '})) heroes.append(Character('๋ฐ๋ํ', 300, 30, {'gold': 300, 'weapon': '์ฅ๊ฒ'})) heroes.append(Character('์ธ๋ฒ๋ฆฐ', 200, 50, {'gold': 350, 'weapon': 'ํด๋ก'})) monsters = [] monsters.append(Character('๊ณ ๋ธ๋ฆฐ', 90, 30, {'gold': 50, 'weapon': '์ฐฝ'})) monsters.append(Character('๋๋๊ณค', 200, 80, {'gold': 200, 'weapon': 'ํ์ผ'})) monsters.append(Character('๋ฑํ์ด์ด', 80, 120, {'gold': 1000, 'weapon': '์ต๋ฉด์ '})) print heroes # ํ์ด๋ก ๋ฆฌ์คํธ ํ์ธ print monsters # ๋ชฌ์คํฐ ๋ฆฌ์คํธ ํ์ธ del heroes[0] # ํ์ด๋ก ๋ฆฌ์คํธ์์ ์์ด์ธ๋งจ ์ญ์ print heroes # ํ์ด๋ก ๋ฆฌ์คํธ ์ฌํ์ธ
[์์ด์ธ๋งจ, ๋ฐ๋ํ, ์ธ๋ฒ๋ฆฐ] [๊ณ ๋ธ๋ฆฐ, ๋๋๊ณค, ๋ฑํ์ด์ด] [๋ฐ๋ํ, ์ธ๋ฒ๋ฆฐ] "Character"๋ผ๋ ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ OOP๋ฅผ ๊ตฌํํด ๋ณด์์ต๋๋ค ์ ์๋ฃ๋ http://schoolofweb.net/blog/posts/%ED%8C%8C%EC%9D%B4%EC%8D%AC-oop-part-1-%EA%B0%9D%EC%B2%B4-%EC%A7%80%ED%96%A5-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8Doop%EC%9D%80-%EB%AC%B4%EC%97%87%EC%9D%B8%EA%B0%80-%EC%99%9C-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94%EA%B0%80/ ์ ์๋ฃ์ ๋๋ค ๋๋๊ณ ์์ ํ ๊ฒ์