Created
July 2, 2017 13:50
-
-
Save 20chan/e98aaa4a0d04eb49e9a607e6890e1c0e to your computer and use it in GitHub Desktop.
The Fun Of Reinvention - Contract 중간 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Contract: | |
@classmethod | |
def check(cls, value): | |
pass | |
class Typed(Contract): | |
@classmethod | |
def check(cls, value): | |
assert isinstance(value, cls.type), f'Expected {cls.type}' | |
super().check(value) | |
class Integer(Typed): | |
type = int | |
class Float(Typed): | |
type = float | |
class String(Typed): | |
type = str | |
class Positive(Contract): | |
@classmethod | |
def check(cls, value): | |
assert value > 0, 'Must be > 0' | |
super().check(value) | |
class PositiveInteger(Integer, Positive): | |
pass | |
PositiveInteger.check(42) # life the universe and everything | |
PositiveInteger.check(-1) | |
PositiveInteger.check("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment