250x250
Link
๋‚˜์˜ GitHub Contribution ๊ทธ๋ž˜ํ”„
Loading data ...
Notice
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Data Science LAB

[Deep Learning] ๋”ฅ๋Ÿฌ๋‹์—์„œ super().__init__ ์‚ฌ์šฉ (ํด๋ž˜์Šค ์ƒ์†) ๋ณธ๋ฌธ

๐Ÿง  Deep Learning

[Deep Learning] ๋”ฅ๋Ÿฌ๋‹์—์„œ super().__init__ ์‚ฌ์šฉ (ํด๋ž˜์Šค ์ƒ์†)

ใ…… ใ…œ ใ…” ใ…‡ 2022. 12. 7. 17:39
728x90

1. ๋”ฅ๋Ÿฌ๋‹์—์„œ ์ž์ฃผ ์“ฐ์ด๋Š” ์ฝ”๋“œ

super().__init__()
super(ClassName, self).__init__()
super().__init__(**kwargs)
super(ClassName, self).__init__(**kwargs)

 

 

 

 

 

 

2. ์‚ฌ์šฉ ์˜ˆ์‹œ

class Parent():
    def __init__(self):
        print("Parent init") 
        self.parentHi = "๋ถ€๋ชจ ํด๋ž˜์Šค"

class Child(Parent):
    def __init__(self):
        super().__init__()    
        print("Child init") 
        self.childHi = "์ž์‹ ํด๋ž˜์Šค"

child = Child()
print(child.childHi)
print(child.parentHi)

# Parent init
# Child init
# ์ž์‹ ํด๋ž˜์Šค
# ๋ถ€๋ชจ ํด๋ž˜์Šค

 

1. ๋ถ€๋ชจ ํด๋ž˜์Šค (Parent) ์„ ์–ธ

2. ์ž์‹ ํด๋ž˜์Šค์—์„œ super.__init__(self) ์˜๋ฏธ : ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ __init__๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ

-> super()๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ __init__() ๋ฉ”์„œ๋“œ์— overriding(๋ฎ์–ด์“ฐ๊ธฐ) ๋จ

3. ์ž์‹ ํด๋ž˜์Šค์— childHI ์ธ์Šคํ„ด์Šค ์ถ”๊ฐ€

 

 

 

- ๋งŒ์•ฝ, ๋ถ€๋ชจ class์— argument(ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜์— ๊ฐ’์„ ์ง‘์–ด๋„ฃ์Œ)์ด ์žˆ๋‹ค๋ฉด **kwargs๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผํ•จ

class Parent():
    def __init__(self,parentHi):
        self.parentHi = parentHi
        print('Parent init')

class Child(Parent):
    def __init__(self,childHi,**kwargs):
        super(Child, self).__init__(**kwargs)
        self.childHi = childHi
        print('Child init')

child = Child(parentHi='๋ถ€๋ชจ', childHi='์ž์‹')
print(child.childHi)
print(child.parentHi)

# Parent init
# Child init
# ์ž์‹
# ๋ถ€๋ชจ

 

**kwargs๊ฐ€ ์—†์œผ๋ฉด error ์ฃผ์˜

728x90
Comments