๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Programming/Python

ํด๋ž˜์Šค(Class)์˜ ์ธ์ž ๋ฐ ๋ฉ”์†Œ๋“œ(Method)

ํŒŒ์ด์ฌ์˜ ์ž๋ฃŒ ๊ตฌ์กฐ์ธ ํด๋ž˜์Šค(Class)์˜ ์ธ์ž ๋ฐ ๋ฉ”์†Œ๋“œ(Method) ๋Œ€ํ•ด ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ๋‹ค๋ฃฐ ๋‚ด์šฉ์œผ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

1. self ์ธ์ž

2. __init__() ๋ฉ”์†Œ๋“œ

3. super() ๋ฉ”์†Œ๋“œ

 

1. self ์ธ์ž

 

In:

class test_class:
    
    def test_fun_1():
        print('Function 1')
        
    def test_fun_2(self):
        print('Function 2')
        
t_c = test_class()

t_c.test_fun_1()

 

Out:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-0e104e8d4d89> in <module>
      1 t_c = test_class()
----> 2 t_c.test_fun_1()

TypeError: test_fun_1() takes 0 positional arguments but 1 was given

 

In:

t_c.test_fun_2()

 

Out:

function 2

 

โ–ท t_c.test_fun_2()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด self๋Š” ํ˜ธ์ถœ์‹œ ์ด์šฉํ–ˆ๋˜ ์ธ์Šคํ„ด์Šค(Instance)๋กœ ๋ฐ”๋€๋‹ค. ๋”ฐ๋ผ์„œ test_fun_2()์— ํ•„์š”ํ•œ self์— ๋Œ€ํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋”ฐ๋กœ ์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋ฐ˜๋ฉด, t_c.test_fun_1()์„ ํ˜ธ์ถœํ•˜๋ฉด ํ˜ธ์ถœ์‹œ ์ด์šฉํ•œ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•จ์ˆ˜๋กœ ๋„˜์–ด๊ฐ€์ง€๋งŒ, ํ•จ์ˆ˜์— ํ•„์š”ํ•œ ์ธ์ž๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

In:

class test_class:

    test_var = True
    
    def test_fun(self):
        self.test_var = False
        
t_c = test_class()

print(t_c.test_var)

t_c.test_fun()

print(t_c.test_var)

 

Out:

True
False

 

โ–ท t_c์—์„œ ํด๋ž˜์Šค ๋‚ด ๋ณ€์ˆ˜ test_var๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. t_c.test_fun()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด  t_c์˜ test_var์ด False๋กœ ๋ฐ”๋€๋‹ค. ๋‹ค๋ฅธ test_class์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, test_var์€ ๊ทธ๋Œ€๋กœ True์ด๋‹ค. ์ธ์Šคํ„ด์Šค์˜ test_var๋งŒ ๋ฐ”๋€Œ์—ˆ์„ ๋ฟ, ํด๋ž˜์Šค ์ž์ฒด์˜ test_var์€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

 

2. __init__() ๋ฉ”์†Œ๋“œ

 

In:

class test_class:
    
    def __init__(self, input):
        self.test_var = input

t_c = test_class('Good!')

print(t_c.test_var)

 

Out:

Good!

 

In:

t_c = test_class()

 

Out:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-89584f52547a> in <module>
----> 1 t_c = test_class()

TypeError: __init__() missing 1 required positional argument: 'input'

 

โ–ท __init__()์€ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•ญ์ƒ ์‹คํ–‰๋œ๋‹ค. ๋”ฐ๋ผ์„œ test_class์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” input์— ๋Œ€ํ•œ ์ธ์ž๋ฅผ ๋ฐ˜๋“œ์‹œ ์ฃผ์–ด์•ผํ•œ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

3. super() ๋ฉ”์†Œ๋“œ

 

In:

class parent_class:
    
    parent_var = True
    
    def parent_function(self):
        print('Parent function')
    
class child_class(parent_class):
    
    def __init__(self):
        super(child_class, self)
        
c_c = child_class()

c_c.parent_function()

print(c_c.parent_var)

 

Out:

Parent function
True

 

โ–ท ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•  ๋•Œ, ์ƒ์†๋ฐ›๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์ธ์ž๋กœ ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ ๋‹ค์Œ super()์— ๋Œ€ํ•œ ์ •์˜๋ฅผ ํ•จ์œผ๋กœ์จ ์ƒ์†๋ฐ›๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜ ๋ฐ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โ–ท child_class์— super(child_class, self)๋ฅผ ์‹คํ–‰ํ•จ์œผ๋กœ์จ parent_class์— ๋Œ€ํ•œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. ๋”ฐ๋ผ์„œ c_c์—์„œ parent_class์˜ ํ•จ์ˆ˜์ธ parent_function()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, parent_class์˜ ๋ณ€์ˆ˜์€ parent_var์— ๋Œ€ํ•œ ์ ‘๊ทผ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.