Skip to content

Instantly share code, notes, and snippets.

@20chan
Created July 2, 2017 13:50
Show Gist options
  • Save 20chan/e98aaa4a0d04eb49e9a607e6890e1c0e to your computer and use it in GitHub Desktop.
Save 20chan/e98aaa4a0d04eb49e9a607e6890e1c0e to your computer and use it in GitHub Desktop.
The Fun Of Reinvention - Contract 중간 코드
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