【发布时间】:2010-04-14 05:48:20
【问题描述】:
我想创建一个自定义数据类型,它的行为基本上类似于普通的int,但其值限制在给定范围内。我想我需要某种工厂函数,但我不知道该怎么做。
myType = MyCustomInt(minimum=7, maximum=49, default=10)
i = myType(16) # OK
i = myType(52) # raises ValueError
i = myType() # i == 10
positiveInt = MyCustomInt(minimum=1) # no maximum restriction
negativeInt = MyCustomInt(maximum=-1) # no minimum restriction
nonsensicalInt = MyCustomInt() # well, the same as an ordinary int
感谢任何提示。谢谢!
【问题讨论】: