Advertisement
Guest User

Untitled

a guest
Oct 9th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 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. class MelonCart:
  12.     honeymelons = Positive('honeymelons')
  13.     watermelons = Positive('watermelons')
  14.  
  15.     def __init__(self, honeymelons, watermelons):
  16.         self.honeymelons = honeymelons
  17.         self.watermelons = watermelons
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement