
class_implements 키워드는 Python 3.3 이상에서 사용할 수 있습니다.
class_implements를 사용하여 클래스가 어떤 인터페이스를 구현하는지 확인하려면, isinstance() 함수와 함께 사용할 수 있습니다.
예를 들어, 아래와 같은 코드를 사용할 수 있습니다.
#hostingforum.kr
python
from abc import ABC, abstractmethod
class MyInterface(ABC):
@abstractmethod
def my_method(self):
pass
class MyClass(MyInterface):
def my_method(self):
print("My method")
print(MyClass.__class__.__bases__[0].__name__ == 'MyInterface') # True
print(MyClass.__class__.__bases__[0].__name__ == 'ABC') # False
또는 isinstance() 함수를 사용할 수 있습니다.
#hostingforum.kr
python
from abc import ABC, abstractmethod
class MyInterface(ABC):
@abstractmethod
def my_method(self):
pass
class MyClass(MyInterface):
def my_method(self):
print("My method")
print(isinstance(MyClass(), MyInterface)) # True
print(isinstance(MyClass(), ABC)) # True
class_implements 키워드는 Python 3.3 이상에서 사용할 수 있습니다. 하지만 isinstance() 함수를 사용하는 방법은 Python 3.0 이상에서 사용할 수 있습니다.
2025-07-23 12:33