일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- data science
- shiba
- 315. Count of Smaller Numbers After Self
- Convert Sorted List to Binary Search Tree
- Generator
- iterator
- Decorator
- 109. Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- Protocol
- 시바견
- attribute
- Regular Expression
- concurrency
- 밴픽
- t1
- 파이썬
- Python Implementation
- 43. Multiply Strings
- DWG
- 운영체제
- 715. Range Module
- kaggle
- LeetCode
- Python
- Class
- Python Code
- 컴퓨터의 구조
- Substring with Concatenation of All Words
- 프로그래머스
Archives
- Today
- Total
Scribbling
Python: Inheritance 본문
Risks of subclassing built-in types
- Generally, overriden methods in sub-classes are not called by other built-in methods.
class StrangeDict(dict):
def __setitem__(self, key, value):
super().__setitem__(key, value*2)
dic = StrangeDict(one=1)
dic['1'] = 1
dic.update(three=3)
print(dic)
- This is because some methods are written with C-language.
- So, use UserDict, UserList, UserString if you need to customize list, dict, str.
- If you need to fully tweak their functionalities, use ABC (from collections.abc).
Multiple Inheritance
class A:
def ping(self):
print('ping', self)
class B(A):
def pong(self):
print('pong', self)
class C(A):
def pong(self):
print('PONG', self)
class D(B, C):
def ping(self):
super().ping()
print('D-ping', self)
def pingpong(self):
self.ping()
super().ping()
self.pong()
super().pong()
C.pong(self)
d = D()
d.pingpong()
'Computer Science > Python' 카테고리의 다른 글
Python: Iterator, Generator (0) | 2022.04.22 |
---|---|
Python: Operator Overloading (0) | 2022.04.20 |
Python: Interfaces (0) | 2022.04.18 |
Python: ABC Class (0) | 2022.04.07 |
Python: Sequence Protocol (0) | 2022.04.06 |