파이썬[Python]: collections.abc - Callable 클래스

collections.abc 모듈 - Callable 클래스(class)


/// 설명

Abstract Base Class 입니다.
# Abstract Methods : __call__
# Minix Methods : None

※ 형식
class collections.abc.Callable

/// 예제

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from collections.abc import Callable
# ABC(Abstract Base Class): Callable
# Abstract Methods : __call__
# Minix Methods : None
 
# first way
# A newly written class can inherit directly from one of the abstract base classes.
class MyClass1(Callable):   # 상속
    def __init__(self):    # 생성자(Extra method)
        pass
 
    def __call__(self):    # 필수(abstract method)
        return 1
 
 
print(issubclass(MyClass1, Callable))  # True
test_instance = MyClass1()
print(isinstance(test_instance, Callable))  # True
 
# second way
# Existing classes and built-in classes can be registered as “virtual subclasses” of the ABCs.
# the full API including all of the abstract methods and all of the mixin methods.
class MyClass2:
    def __init__(self):    # 생성자(Extra method)
        pass
 
    def __call__(self):    # 필수(abstract method)
        return 1
 
 
Callable.register(MyClass2)
 
print(issubclass(MyClass2, Callable))  # True
test_instance = MyClass2()
print(isinstance(test_instance, Callable))  # True
 
# third way
# Some simple interfaces are directly recognizable by the presence of the required methods.
class MyClass3:
    def __call__(self):    # 필수(abstract method)
        return 1
 
 
print(issubclass(MyClass2, Callable))  # True
test_instance = MyClass2()
print(isinstance(test_instance, Callable))  # True
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수