Programming/Python

클래슀(Class)의 인자 및 λ©”μ†Œλ“œ(Method)

🌻Pep🌻 2020. 7. 26. 17:27

파이썬의 자료 ꡬ쑰인 클래슀(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에 λŒ€ν•œ 접근도 κ°€λŠ₯ν•˜λ‹€.