-
python MixInpython/응용 2024. 1. 31. 17:07
scala 의 trait mixin 과 비슷한 것 같다. 상속을 통해 공통으로 사용할 member, method 를 클래스에 추가함?
예제 코드
class ToDictMixin: def to_dict(self): return {key: value for key, value in self.__dict__.items()} class MyClass(ToDictMixin, BaseClass): pass
다중 상속일 경우
Q: Under multiple inheritances, if two parent classes have the same methods or attributes, what will happen?
A: If two parent classes have the same method or attribute, the method or attribute in the class that appears first in the inheritance list will take precedence.
예; MyClass(A, B) 에서 A class 와 B class 메서드 충돌이 있을 경우 A class 메서드가 사용됨.
'python > 응용' 카테고리의 다른 글
functools.singledispatch 로 typeclass 흉내 내기 (0) 2024.01.24 Coroutine yield 의 동작 이해 (0) 2023.12.13 yield 활용 2 (0) 2023.11.15 yield 활용 1 (0) 2023.11.15 Decorators with parameters (0) 2023.10.17