끄적끄적 코딩
Published 2020. 9. 15. 04:22
[파이썬] pass, super Python
728x90

pass

<python />
class test(): def __init__(self): pass def abc(): pass

pass는 클래스나 함수를 완성하지 않은 상태에서 오류가 나지 않게 보류해두는 상태를 만듭니다.

 

super

<python />
class People: def __init__(self): print("People 생성자") class detailPeople(People): def __init__(self): #People.__init__(self) super().__init__()

super를 사용해서 부모클래스에 접근이 가능합니다.
위의 코드처럼 부모 클래스 대신에 super로 사용이 가능하며, self도 생략이 가능합니다.

super를 썼을때의 단점으로 다중상속을 받을 경우 super가 두 부모 클래스 중
어떤 클래스를 지칭하는지 프로그램이 알기가 어렵습니다.

<python />
class People: def __init__(self): print("People 생성자") class Car: def __init__(self): print("Car 생성자") class detailPeople(People, Car): def __init__(self): super().__init__() test = detailPeople()

출력 결과
People 생성자

위의 예제처럼 다중 상속일 때 super를 사용하게 될 경우 다중 상속에서 먼저 선언한 클래스가 super가 가르키게 됩니다.
그래서 다중상속을 사용할 때는 super를 사용하지 않고 클래스를 직접 입력해서 생성자를 만들어주는것이 좋습니다.

 

'Python' 카테고리의 다른 글

[파이썬] raise  (0) 2020.09.15
[파이썬] 예외처리 (try, except, finally)  (0) 2020.09.15
[파이썬] 메소드 오버라이딩  (0) 2020.09.15
[파이썬] 상속, 다중 상속  (0) 2020.09.15
[파이썬] 클래스  (0) 2020.09.15

검색 태그