일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Convert Sorted List to Binary Search Tree
- Generator
- 30. Substring with Concatenation of All Words
- 컴퓨터의 구조
- 315. Count of Smaller Numbers After Self
- attribute
- Regular Expression
- DWG
- Class
- 밴픽
- 시바견
- Substring with Concatenation of All Words
- concurrency
- t1
- 43. Multiply Strings
- 프로그래머스
- Python
- 운영체제
- 파이썬
- iterator
- Decorator
- Protocol
- 715. Range Module
- data science
- Python Implementation
- LeetCode
- Python Code
- shiba
- 109. Convert Sorted List to Binary Search Tree
- kaggle
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 |