| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 29 | 30 | 31 |
Tags
- LeetCode
- kaggle
- shiba
- 운영체제
- Regular Expression
- 시바견
- 밴픽
- Convert Sorted List to Binary Search Tree
- Python Implementation
- 30. Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- iterator
- 43. Multiply Strings
- Decorator
- Python
- 315. Count of Smaller Numbers After Self
- Python Code
- 프로그래머스
- Generator
- attribute
- data science
- 파이썬
- 109. Convert Sorted List to Binary Search Tree
- Protocol
- DWG
- 715. Range Module
- 컴퓨터의 구조
- Class
- t1
- concurrency
Archives
- Today
- Total
Scribbling
Python: ABC Class 본문
The main goals we use 'Abstract Base Class' in python are as follows:
- to provide a standerdized way to test whether an object adheres to a certain specification
- to prevent any attempt to initiate a subclass that does not override methods of the super class
1> To provide a standerdized way to test whether an object adheres to a certain specification
import abc
class TypeGroup1(metaclass=abc.ABCMeta):
pass
@TypeGroup1.register
class UniqueObject:
pass
TypeGroup1.register(list)
TypeGroup1.register(tuple)
def iamfunc(object):
if isinstance(object, TypeGroup1):
print("Passed object is in TypeGroup1")
else:
print("None")
iamfunc([])
iamfunc((3, 4))
iamfunc(UniqueObject())
iamfunc(dict())

2> To prevent any attempt to initiate a subclass that does not override methods of the super class
import abc
class Abstract(metaclass=abc.ABCMeta):
@abc.abstractmethod
def func(self):
pass
class Derived1(Abstract):
def func(self):
pass
class Derived2(Abstract):
pass
d = Derived1()
d = Derived2()

Among many advantages of using it, the program raises error during import time can be especially helpful.
'Computer Science > Python' 카테고리의 다른 글
| Python: Inheritance (0) | 2022.04.19 |
|---|---|
| Python: Interfaces (0) | 2022.04.18 |
| Python: Sequence Protocol (0) | 2022.04.06 |
| Python: Pythonic Object (0) | 2022.04.05 |
| Python: Object References (0) | 2022.04.04 |