๐Ÿฅ Python 03. ๋ฐ˜๋ณต๋ฌธ, class

yeeun leeยท2020๋…„ 3์›” 26์ผ
0

Python Basic

๋ชฉ๋ก ๋ณด๊ธฐ
4/6
post-custom-banner

1. iteration

ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ฝ”๋“œ ๋ธ”๋ก์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋กœ์ง. ๋ณดํ†ต ๋ฐ˜๋ณต์ž๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ, python์€ ์ „ํ†ต์ ์ธ for loop๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๋‹ค. python์€ javascript ์ฒ˜๋Ÿผ initializer, condition ๋˜๋Š” iterator ๋ถ€๋ถ„์ด ์—†๋‹ค.

  • initializer: loop์— ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋œ๋‹ค.
  • condition: ์กฐ๊ฑด ๋ถ€๋ถ„์€ boolean expression์œผ๋กœ ํ‘œํ˜„๋˜์–ด์•ผ ํ•œ๋‹ค.
  • iterator: ๋ฐ˜๋ณต ๋’ค์— ์‹คํ–‰๋  ๋ถ€๋ถ„
for (initializer; condition; iterator)
    body
    
//javascript for loop
let numbers = [10, 12, 15, 18, 20];
for (let i = 0; i < numbers.length; i += 1) {
    console.log(numbers[i])
}
# python for loop
numbers = [10, 12, 15, 18, 20]
for number in numbers:
    print(number) # list์— ์žˆ๋Š” ์š”์†Œ ํ•˜๋‚˜์”ฉ print ํ•ด์ฃผ์„ธ์š”

iterable

member๋ฅผ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” object๋ฅผ ๋งํ•œ๋‹ค. iterable์ด iter() method๋ฅผ passํ•  ๋•Œ iterator๋ฅผ ๋งŒ๋“ค๊ณ , __next__ method๋ฅผ ์‚ฌ์šฉํ•ด iterable ๊ฐ์ฒด๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค. iterator๋Š” __next__ method๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ณ , ๊ฐ์ฒด์˜ ๋‹ค์Œ ์•„์ดํ…œ์„ ๋ฆฌํ„ดํ•œ๋‹ค.

sequence๊ฐ€ iterable type์˜ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์˜ˆ๋‹ค. built-in sequnece type์€ list, string, tuple์ด๋‹ค. ๊ทธ๋ฐ–์—๋„ ๋”•์…”๋„ˆ๋ฆฌ, ํŒŒ์ผ ๊ฐ์ฒด, set, generator๊ฐ€ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•˜์ง€๋งŒ sequence๋Š” ์•„๋‹ˆ๋‹ค.

Note that every iterator is also an iterable, but not every iterable is an iterator.

๋ชจ๋“  ๋ฐ˜๋ณต์ž๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๊ฐ€ ๋ชจ๋‘ ๋ฐ˜๋ณต์ž๋Š” ์•„๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ๋ฆฌ์ŠคํŠธ๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋ฐ˜๋ณต์ž๋Š” ์•„๋‹ˆ๋‹ค. iterator๋Š” iterable์— iter() ํ•จ์ˆ˜๋ฅผ ์จ์„œ ๋งŒ๋“ค์–ด์ง„๋‹ค. ์ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๊ฐ€ __iter__method(iterator๋ฅผ return)์™€ 0์œผ๋กœ ์‹œ์ž‘ํ•˜๊ณ  ์—ฐ์†์  ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ __getitem method๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

1.1 for loops

list, set, dic ๋“ฑ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ด๋‹น ์š”์†Œ๋ฅผ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ์›ํ•˜๋Š” ๋กœ์ง์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

- break

์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๊ณ  for๊ตฌ๋ฌธ์—์„œ ์™„์ „ํžˆ ๋น ์ ธ๋‚˜์˜ค๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

my_list=[1,2,3,4,5]
for element in my_list:
    if condition:
    	do something
        break # ๋ฐ˜๋ณต๋ฌธ์„ ๋ฉˆ์ถ”๋Š” ๋ฐฉ๋ฒ• 
    else:
        do_something_with_element

- continue

break ์ฒ˜๋Ÿผ ์™„์ „ํžˆ ๋น ์ ธ๋‚˜์˜ค์ง„ ์•Š์ง€๋งŒ, ๋‹ค์Œ iteration์œผ๋กœ ๋„˜์–ด๊ฐ€๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

cats = ["๋งˆ๋ธ”", "์น˜์ฆˆ", "์นด์˜ค์Šค", "์บกํ‹ด", "ํ„ฑ์‹œ๋„"]

for cat in cats:
   if cat !="์บกํ‹ด":
      print("์ง€๊ธˆ์€ ์บกํ‹ด ๋ฐฅ์ค„ ์‹œ๊ฐ„")
      continue
   else:
      print("์บกํ‹ด ๋ฐฅ๋จน์ž!") 
    
'''result:
์ง€๊ธˆ์€ ์บกํ‹ด ๋ฐฅ์ค„ ์‹œ๊ฐ„
์ง€๊ธˆ์€ ์บกํ‹ด ๋ฐฅ์ค„ ์‹œ๊ฐ„
์ง€๊ธˆ์€ ์บกํ‹ด ๋ฐฅ์ค„ ์‹œ๊ฐ„
์บกํ‹ด ๋ฐฅ๋จน์ž!
์ง€๊ธˆ์€ ์บกํ‹ด ๋ฐฅ์ค„ ์‹œ๊ฐ„
'''

- nested for loops

python์€ loop ์•ˆ์—์„œ ๋˜ ํ•˜๋‚˜์˜ loop๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

numbers1 = [1, 2, 3, 4, 5]
numbers2 = [10, 20, 30, 40, 50]

for num1 in numbers1:
    for num2 in numbers2:
        print(f"{num1} * {num2} == {num1 * num2}")

1.2 while loops

์ž๋ฃŒ ๊ตฌ์กฐ ๊ธฐ๋ฐ˜์ด ์•„๋‹ˆ๋”๋ผ๋„ ์ฝ”๋“œ ๋ธ”๋ก์„ ๋ฐ˜๋ณตํ•ด์•ผ ํ•  ๋•Œ. ํŠน์ • ์กฐ๊ฑด๋ฌธ์ด True์ผ ๋™์•ˆ ์ฝ”๋“œ๋ธ”๋ก์„ ๋ฐ˜๋ณต ์‹คํ–‰ํ•œ๋‹ค. False์ด๋ฉด ๋ฉˆ์ถ˜๋‹ค. for๋ฌธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ break์™€ continue๊ฐ€ ์žˆ๋‹ค.

while <์กฐ๊ฑด๋ฌธ>:     
    <์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ1>     
    <์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ2>     
    ...
    <์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅN>

- while else

if๋ฌธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ while์˜ ์กฐ๊ฑด๋ฌธ์ด false์ด๋ฉด ์‹คํ–‰๋œ๋‹ค. ์ฆ‰, while๋ฌธ์ด ์ข…๋ฃŒ๋˜๋ฉด else๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค๋Š” ๋œป์ด๋‹ค.

while <์กฐ๊ฑด๋ฌธ>:
    <์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ1>     
    <์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ2>     
    <์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ3>     
    ...
    <์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅN> 
else:
    <while๋ฌธ์ด ์ข…๋ฃŒ๋œ ํ›„ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ>          

1.3 looping dictionary

dictionary๋ฅผ ์‚ฌ์šฉํ•œ for๋ฌธ์—์„œ๋Š” ๊ฐ ์š”์†Œ์˜ key๋งŒ ๋ฆฌํ„ดํ•˜๊ณ , key๊ฐ’์„ ํ†ตํ•ด value๋ฅผ ์ฝ์–ด๋“ค์ด๋Š” ๊ตฌ์กฐ๋‹ค.

cats = [ "์ฒซ์งธ":"์บกํ‹ด", "๋‘˜์งธ":"๋งˆ๋ธ”"]
for cat in cats:
	print(f'{cat} ๊ณ ์–‘์ด๋Š” {cats[cat]}์ž…๋‹ˆ๋‹ค')

'''result:
์ฒซ์งธ ๊ณ ์–‘์ด๋Š” ์บกํ‹ด์ž…๋‹ˆ๋‹ค
๋‘˜์งธ ๊ณ ์–‘์ด๋Š” ๋งˆ๋ธ”์ž…๋‹ˆ๋‹ค
''' 
    

- value()

dict์€ value๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ์“ธ ํ•„์š” ์—†์ด, ๋ฐ˜๋ณต๋˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๊ฐ’์„ ๋ฐ”๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

cats = [ "์ฒซ์งธ":"์บกํ‹ด", "๋‘˜์งธ":"๋งˆ๋ธ”"]
for cat in cats.value():
	print(f'๊ณ ์–‘์ด ์ด๋ฆ„์€ {cat}์ž…๋‹ˆ๋‹ค')

'''result:
๊ณ ์–‘์ด ์ด๋ฆ„์€ ์บกํ‹ด์ž…๋‹ˆ๋‹ค
๊ณ ์–‘์ด ์ด๋ฆ„์€ ๋งˆ๋ธ”์ž…๋‹ˆ๋‹ค
'''  

- item()

key์™€ value ๊ฐ’ ๋‘˜ ๋‹ค ๊ฐ€์ง€๊ณ  for ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. key์™€ value๋ฅผ ํŠœํ”Œ๋กœ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

cats = [ "์ฒซ์งธ":"์บกํ‹ด", "๋‘˜์งธ":"๋งˆ๋ธ”"]
for cats_key, cats_value in cats.items():
	print(f'{cats_key} ๊ณ ์–‘์ด๋Š” {cats_value}์ž…๋‹ˆ๋‹ค')

'''result:
์ฒซ์งธ ๊ณ ์–‘์ด๋Š” ์บกํ‹ด์ž…๋‹ˆ๋‹ค
๋‘˜์งธ ๊ณ ์–‘์ด๋Š” ๋งˆ๋ธ”์ž…๋‹ˆ๋‹ค
''' 
    

2. scope

Scope์€ ๋ฒ”์œ„๋ผ๋Š” ๋œป์ด๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ scope์€ ์–ด๋– ํ•œ ๊ฐ์ฒด (๋ณ€์ˆ˜, ํ•จ์ˆ˜ ๋“ฑ)๊ฐ€ ์œ ํšจํ•œ ๋ฒ”์œ„๋ฅผ ๋งํ•˜๊ณ , ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

Python์—์„œ scope์€ ํ•ญ์ƒ ๊ฐ์ฒด๊ฐ€ ์„ ์–ธ๋œ ์ง€์ ์—์„œ ์œ„๋กœ๋Š” ์ƒ์œ„ ๊ฐ์ฒด ๊นŒ์ง€, ์•„๋ž˜๋กœ๋Š” ๋ชจ๋“  ํ•˜์œ„ ๊ฐ์ฒด๋“ค๊ณผ ๊ทธ ์•ˆ์—๊นŒ์ง€๊ฐ€ ๋ฒ”์œ„์ด๋‹ค.

์ˆœ์„œ: Local => Enclosing => Global => Built-in

  • Local Scope: ํŠน์ • ๋ฒ”์œ„์—์„œ๋งŒ ์œ ํšจํ•œ ๊ฐ์ฒด. ์ฃผ๋กœ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜.
  • Enclosed Scope: ์ค‘์ฒฉ ํ•จ์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ ์ ์šฉ๋˜๋Š” scope. ๋ถ€๋ชจ ํ•จ์ˆ˜์—์„œ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” ์ค‘์ฒฉ ํ•จ์ˆ˜ ์•ˆ์—์„œ๋„ ์œ ํšจํ•˜๋‹ค.
  • Global Scope: ํ•จ์ˆ˜ ๋ฐ–์—์„œ ์„ ์–ธ๋œ ๊ฐ์ฒด. ๊ฐ์ฒด๋Š” ์„ ์–ธ๋œ ์ง€์ ๊ณผ ๋™์ผํ•œ level์˜ ์ง€์—ญ, ๋” ์•ˆ์ชฝ๊นŒ์ง€ ๋ฒ”์œ„๊ฐ€ ์œ ํšจํ•˜๋‹ค.
  • Built-in Scope: ๊ฐ€์žฅ ๊ด‘๋ฒ”์œ„ํ•œ scope. ๋‚ด์žฅ ํ•จ์ˆ˜์™€ ์†์„ฑ์ด built-in scope๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

shadowing

๋™์ผํ•œ ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๊ฐ€ ๋‹ค๋ฅธ scope์—์„œ ์„ ์–ธ๋˜๋ฉด, ๋” ์ข์€ ๋ฒ”์œ„์˜ ๊ฐ์ฒด๊ฐ€ ๋„“์€ ๋ฒ”์œ„์˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌ๋Š” ํšจ๊ณผ.

3. class

class๋Š” instance๋ฅผ ์ฐ์–ด๋‚ด๋Š” ํ‹€. ์ฟ ํ‚ค๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ, class๋ผ๋Š” ํ‹€์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๋ง›์˜ ์ฟ ํ‚ค์ธ instance๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ํ† ํ•‘์— ๋”ฐ๋ผ ๋ง›์€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์ง€๋งŒ ๋ชจ๋‘ ์ฟ ํ‚ค๋ผ๋Š” ๊ณตํ†ต์ ์„ ๊ฐ–๊ณ  ์žˆ๋‹ค.

3.1 ์ •์˜

class ClassName:
    ...class ๋‚ด์šฉ ์ฝ”๋“œ๋“ค
    
class1 = ClassName()
class2 = ClassName()
  • class ์ด๋ฆ„์€ ๊ฐ ๋‹จ์–ด์˜ ์•ž๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
  • ํ•œ ๋‹จ์–ด ์ด์ƒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๋ฉด ๋ฐ‘์ค„ ์—†์ด ๋‹จ์–ด ์•ž๋ถ€๋ถ„์„ ๋Œ€๋ฌธ์ž๋กœ ์ด์–ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
  • class ์•ˆ์—์„œ ์ •์˜ํ•˜๋Š” ํ•จ์ˆ˜๋Š” method๋ผ๊ณ  ํ•œ๋‹ค.

3.2 attribute & method

class Car:
    def __init__(self, maker, model, horse_power): #attribute
        self.maker       = maker
        self.model       = model
        self.horse_power = horse_power
        
    def honk(self): # method
        return "๋น ๋ผ๋ฐ”๋ผ๋น ๋ผ๋ฐค"
        
    def honk_with_name(self): 
    	return f'{self.maker} ๋น ๋ผ๋ฐ”๋ผ๋น ๋ผ๋ฐค' #๊ฐ์ฒด์˜ attribute์ธ self.maker ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

hyundai = Car("ํ˜„๋Œ€", "์ œ๋„ค์‹œ์Šค", 500) # instance ๊ฐ์ฒด ์ƒ์„ฑ
     
hyundai.honk() # calling method 
> "๋น ๋ผ๋ฐ”๋ผ๋น ๋ผ๋ฐค"

hyundai.honk_with_name()
> "ํ˜„๋Œ€ ๋น ๋ผ๋ฐ”๋ผ๋น ๋ผ๋ฐค"

- __init__

class๊ฐ€ ์‹ค์ฒดํ™”๋  ๋•Œ(instance๋ฅผ ๋งŒ๋“ค ๋•Œ) ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. instance๋ฅผ ๋งŒ๋“ค ๋•Œ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋œ๋‹ค.

class Database:
  def __init__(self, name, size):
    self.name = name
    self.size = size
    self.dict = {}

underscores in python

์•ž ๋’ค๋กœ ๋ฐ‘์ค„ 2๊ฐœ๊ฐ€ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ special methods๋ผ๊ณ  ํ•œ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ์ด๋ฆ„๊ณผ ์ถฉ๋Œํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ ์“ฐ๋Š” convention์ด๋‹ค.

- self

self๋Š” class์˜ ์‹ค์ฒด(instance)์ธ ๊ฐ์ฒด(object)๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค. Car class์—์„œ "self" ๋Š” Car class์˜ ๊ฐ์ฒด์ธ hyundai๋‚˜ bmw๋ฅผ ๊ฐ€๋ฅดํ‚ค๋Š” ๊ฒƒ์ด๋‹ค. ์•„๋ž˜ ์„ธ ์ค„์€ ์ด๋“ค์„ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด maker, model, horse_power๋ฅผ ๊ฐ์ฒด์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

- method parameter

class์˜ method๋ฅผ ๋งŒ๋“ค ๋•Œ, ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ•ญ์ƒ self ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค. ๊ทธ ๋‹ค์Œ์œผ๋กœ ์ถ”๊ฐ€๋˜๋Š” ๋ถ€๋ถ„์€ ํŒŒ๋ผ๋ฏธํ„ฐ์ด๊ณ (instance ์ƒ์„ฑ ์‹œ ์ธ์ž๊ฐ€ ๋“ค์–ด๊ฐˆ), __init__์—์„œ ์ •์˜ํ•œ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด dot notation์„ ์‚ฌ์šฉํ•œ๋‹ค.

def insert(self, field, value):
  if len(self.dict.keys()) < self.size:
    self.dict[field] = value
  else:
    pass 
    
def select(self, field):
  if field in self.dict:
    return self.dict[field]
  else:
    return None

None

Data type ์ค‘ ํ•˜๋‚˜. null or no value๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ์“ฐ์ธ๋‹ค. 0์ด๋‚˜ False๋ฅผ ์˜๋ฏธํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ž์‹ ๋งŒ์˜ value๋ฅผ ๊ฐ€์ง„ data type์ด๊ณ , Python์€ ์˜ค์ง ํ•˜๋‚˜์˜ None ๊ฐ์ฒด๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ณ€์ˆ˜์— None์„ ํ• ๋‹นํ•˜๋ฉด ๋ชจ๋“  ๋ณ€์ˆ˜๊ฐ€ ๋™์ผํ•œ id๋ฅผ ๊ฐ–๊ฒŒ ๋œ๋‹ค.

a=None
b=None
c=None

print(id(a)) # 4458713192
print(id(b)) # 4458713192
print(id(c)) # 4458713192
>>> 

3.3 overriding

๋‹ค์Œ ๊ฐœ๋…์€ ํŒŒ์ด์ฌ ์ฝ”๋”ฉ ๋„์žฅ์—์„œ ์ฐธ๊ณ ํ–ˆ๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋”ฉ(overriding)์€ ๋ฌด์‹œํ•˜๋‹ค, ์šฐ์„ ํ•˜๋‹ค๋ผ๋Š” ๋œป์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ๋ง ๊ทธ๋Œ€๋กœ ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ method๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ์ƒˆ๋กœ์šด method๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ๋œป์ด๋‹ค. ์•„๋ž˜์—์„œ Person ํด๋ž˜์Šค์˜ greeting method๋ฅผ ๋ฌด์‹œํ•˜๊ณ  Student ํด๋ž˜์Šค์—์„œ ์ƒˆ๋กœ์šด greeting method๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

class Person:
    def greeting(self):
        print('์•ˆ๋…•ํ•˜์„ธ์š”.')
 
class Student(Person):
    def greeting(self):
        print('์•ˆ๋…•ํ•˜์„ธ์š”. ์ €๋Š” ํŒŒ์ด์ฌ ์ฝ”๋”ฉ ๋„์žฅ ํ•™์ƒ์ž…๋‹ˆ๋‹ค.')
 
james = Student()
james.greeting() # result:์•ˆ๋…•ํ•˜์„ธ์š”. ์ €๋Š” ํŒŒ์ด์ฌ ์ฝ”๋”ฉ ๋„์žฅ ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

์‚ฌ์šฉ ์ด์œ 

๊ทธ๋Ÿผ method ์˜ค๋ฒ„๋ผ์ด๋”ฉ์€ ์™œ ์‚ฌ์šฉํ• ๊นŒ? ๋ณดํ†ต ํ”„๋กœ๊ทธ๋žจ์—์„œ ์–ด๋–ค ๊ธฐ๋Šฅ(์œ„์˜ ์˜ˆ๋ฅผ ๋“ค๋ฉด ์ธ์‚ฌ)์ด ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ๊ณ„์† ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•  ๋•Œ method ์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ํ™œ์šฉํ•œ๋‹ค.

๋งŒ์•ฝ method์˜ ๋‚ด์šฉ์ด ์ค‘๋ณต๋  ๊ฒฝ์šฐ, ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌํ™œ์šฉํ•˜๋ฉด ์ค‘๋ณต์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์˜ค๋ฒ„๋ผ์ด๋”ฉ๋œ ๋ฉ”์„œ๋“œ์—์„œ super()๋กœ ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

class Student(Person):
    def greeting(self):
        super().greeting() # ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ•˜์—ฌ ์ค‘๋ณต์„ ์ค„์ž„
        print('์ €๋Š” ํŒŒ์ด์ฌ ์ฝ”๋”ฉ ๋„์žฅ ํ•™์ƒ์ž…๋‹ˆ๋‹ค.')
 
james = Student()
james.greeting()

#result:
#์•ˆ๋…•ํ•˜์„ธ์š”
#์ €๋Š” ํŒŒ์ด์ฌ ์ฝ”๋”ฉ ๋„์žฅ ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

์œ„์™€ ๊ฐ™์ด ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์›๋ž˜ ๊ธฐ๋Šฅ์€ ์œ ์ง€ํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ๋ง๋ถ™์ผ ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

profile
์ด์‚ฌ๊ฐ„ ๋ธ”๋กœ๊ทธ: yenilee.github.io
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€