특수 값 "EMPTY"및 "UNIVERSE"가있는 Python 클래스가 있습니다.
class RealSet:
"""Continuous open, half-open, and closed regions and discreet values of the Reals"""
# implementation placeholder
def __init__(self, intervals, *, canonicalize):
pass
# Outside the class
RealSet.EMPTY = RealSet(tuple(), canonicalize=False) # type: ignore
RealSet.UNIVERSE = RealSet(((None, None),), canonicalize=False) # type: ignore
그러나 linting, 코드 완성 등은 클래스의 정적 속성으로 간주되지 않기 때문에 이것을 좋아하지 않습니다. 설정조차도 mypy 오류로보고되므로 # type: ignore
.
아래는 RealSet
아직 존재하지 않기 때문에 클래스 범위에서 구성 할 수 없기 때문에 작동 하지 않습니다.
class RealSet:
"""Continuous open, half-open, and closed regions and discreet values of the Reals"""
...
...
EMPTY = RealSet(tuple(), canonicalize=False) # error
UNIVERSE = RealSet(((None, None),), canonicalize=False) # error
그리고 이것은 클래스 속성이 아닌 인스턴스 속성을 정의하기 때문에 작동하지 않습니다.
class RealSet:
"""Continuous open, half-open, and closed regions and discreet values of the Reals"""
...
...
EMPTY: "RealSet"
UNIVERSE: "RealSet"
# Outside the class
RealSet.EMPTY = RealSet(tuple(), canonicalize=False)
RealSet.UNIVERSE = RealSet(((None, None),), canonicalize=False)
이것은 파이썬 클래스 디자인의 코너 케이스 인 것 같습니다. 속성의 유형이 해당 속성이있는 클래스 인 클래스 속성을 어떻게 만들 수 있습니까? 보너스 : 그것들을 일정하게 만드십시오.
를 사용 typing.ClassVar
하여 클래스 변수에 주석을 달 수 있습니다 .
class RealSet:
def __init__(self, intervals, *, canonicalize):
pass
EMPTY: ClassVar['RealSet']
UNIVERSE: ClassVar['RealSet']
RealSet.EMPTY = RealSet(tuple(), canonicalize=False)
RealSet.UNIVERSE = RealSet(((None, None),), canonicalize=False)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다