"super()"는 파이썬에서 사용되는 내장 함수입니다. 이 함수는 부모 클래스의 메서드를 호출하기 위해 사용됩니다.
부모 클래스를 상속받은 자식 클래스에서 "super()"를 사용하면 부모 클래스의 메서드를 호출할 수 있습니다. 이는 자식 클래스에서 부모 클래스의 생성자나 다른 메서드를 호출할 때 자주 사용됩니다.
"super()" 함수는 두 개의 인수를 받을 수 있습니다. 첫 번째 인수로는 자식 클래스의 이름을 전달하고, 두 번째 인수로는 self라는 특수한 인수를 전달합니다. 자식 클래스에서 "super()"를 호출하면 부모 클래스 내에서 self 인수로 지정된 인스턴스를 참조할 수 있게 됩니다. 이를 통해 부모 클래스의 메서드나 속성에 접근할 수 있습니다.
아래는 "super()" 함수의 예시 코드입니다:
```python
class ParentClass:
def __init__(self):
self.parent_var = "Parent Variable"
def parent_method(self):
print("Parent Method")
class ChildClass(ParentClass):
def __init__(self):
super().__init__() # ParentClass의 생성자 호출
self.child_var = "Child Variable"
def child_method(self):
super().parent_method() # ParentClass의 parent_method 호출
print("Child Method")
# ChildClass의 인스턴스 생성
child_obj = ChildClass()
# 부모 클래스의 속성과 메서드에 접근
print(child_obj.parent_var) # "Parent Variable" 출력
child_obj.parent_method() # "Parent Method" 출력
# 자식 클래스의 속성과 메서드에 접근
print(child_obj.child_var) # "Child Variable" 출력
child_obj.child_method() # "Parent Method"와 "Child Method" 출력
```
위의 코드에서, "super()" 함수를 사용하여 "ChildClass"에서 "ParentClass"의 생성자를 호출하고 부모 클래스의 속성과 메서드에 접근합니다. 또한, "ChildClass"에서 "parent_method" 메서드를 호출할 때도 "super()" 함수를 사용하여 부모 클래스의 메서드를 호출합니다.
자세한 내용은 파이썬 공식 문서에서 확인할 수 있습니다: [Python Documentation - super()](https://docs.python.org/3/library/functions.html#super)
댓글