반응형
파이썬 추상 클래스에서 추상 속성을 만드는 방법
다음 코드에서는 기본 추상 클래스를 만듭니다 Base
. 상속하는 모든 클래스가 속성 Base
을 제공하기 를 원하므로이 name
속성을 @abstractmethod
.
그런 다음 일부 기능을 제공하지만 여전히 추상적으로 유지되는 Base
라는 하위 클래스를 만들었습니다 Base_1
. 에는 name
속성이 Base_1
없지만 그럼에도 불구하고 파이썬은 오류없이 해당 클래스의 객체를 instatinates. 추상 속성을 어떻게 생성합니까?
from abc import ABCMeta, abstractmethod
class Base(object):
__metaclass__ = ABCMeta
def __init__(self, strDirConfig):
self.strDirConfig = strDirConfig
@abstractmethod
def _doStuff(self, signals):
pass
@property
@abstractmethod
def name(self):
#this property will be supplied by the inheriting classes
#individually
pass
class Base_1(Base):
__metaclass__ = ABCMeta
# this class does not provide the name property, should raise an error
def __init__(self, strDirConfig):
super(Base_1, self).__init__(strDirConfig)
def _doStuff(self, signals):
print 'Base_1 does stuff'
class C(Base_1):
@property
def name(self):
return 'class C'
if __name__ == '__main__':
b1 = Base_1('abc')
Python 3.3 이후 로 버그가 수정되어 property()
추상 메서드에 적용될 때 데코레이터가 이제 추상으로 올바르게 식별됩니다.
참고 : 주문이 중요하므로 @property
이전 에 사용해야합니다.@abstractmethod
Python 3.3 이상 : ( python docs ) :
class C(ABC):
@property
@abstractmethod
def my_abstract_property(self):
...
파이썬 2 : ( 파이썬 문서 )
class C(ABC):
@abstractproperty
def my_abstract_property(self):
...
때까지 파이썬 3.3 , 당신은 중첩 할 수 없습니다 @abstractmethod
와 @property
.
@abstractproperty
추상 속성을 만드는 데 사용 합니다 ( docs ).
from abc import ABCMeta, abstractmethod, abstractproperty
class Base(object):
# ...
@abstractproperty
def name(self):
pass
이제 코드는 올바른 예외를 발생시킵니다.
역 추적 (가장 최근 호출 마지막) : 파일 "foo.py", 36 행, b1 = Base_1 ( 'abc') TypeError : 추상 메서드 이름으로 Base_1 추상 클래스를 인스턴스화 할 수 없습니다.
위의 James 답변을 기반으로
def compatibleabstractproperty(func):
if sys.version_info > (3, 3):
return property(abstractmethod(func))
else:
return abstractproperty(func)
데코레이터로 사용
@compatibleabstractproperty
def env(self):
raise NotImplementedError()
반응형
'Development Tip' 카테고리의 다른 글
Akka의 Actor와 Scala의 Actor 모델의 차이점은 무엇입니까? (0) | 2020.10.24 |
---|---|
API를 사용하여 기본 MailChimp 가입 양식 만들기 (0) | 2020.10.24 |
파이썬 set ([])은 두 객체가 같은지 어떻게 확인합니까? (0) | 2020.10.24 |
google-services.json (Firebase에서)을 내 저장소에 추가해야하나요? (0) | 2020.10.24 |
대규모 코드베이스에 익숙해지는 가장 좋은 방법은 무엇입니까? (0) | 2020.10.24 |