Guest User

Untitled

a guest
Oct 9th, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. class _Positive:
  2.     def __init__(self, attr):
  3.         self.attr = attr # the managed attribute
  4.         self.msg = "can't have less than zero of {}!".format(attr)
  5.  
  6.     def __set__(self, instance, value):
  7.         if value < 0:
  8.             raise ValueError(self.msg)
  9.         vars(instance)[self.attr] = value
  10.  
  11. def positive(*attrs):
  12.     def set_attrs(cls):
  13.         for attr in attrs:
  14.             setattr(cls, attr, _Positive(attr))
  15.         return cls
  16.     return set_attrs
  17.  
  18. @positive('honeymelons', 'watermelons')
  19. class MelonCart:
  20.     def __init__(self, honeymelons, watermelons):
  21.         self.honeymelons = honeymelons
  22.         self.watermelons = watermelons
Add Comment
Please, Sign In to add comment