SHARE
TWEET

Untitled

a guest May 15th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def quantity(property_name):
  2.     """Property factory method"""
  3.    
  4.     def qty_getter(instance):
  5.         return instance.__dict__[property_name]
  6.    
  7.     def qty_setter(instance, value):
  8.         if value > 0:
  9.             instance.__dict__[property_name] = value
  10.         else:
  11.             raise ValueError('value must be > 0')
  12.    
  13.     return property(qty_getter, qty_setter)
  14.  
  15. class LineItem:
  16.     weight = quantity('weight')
  17.     price = quantity('price')
  18.    
  19.     def __init__(self, description, weight, price):
  20.         self.description = description
  21.         self.weight = weight
  22.         self.price = price
  23.        
  24.     def subtotal(self):
  25.         return self.weight * self.price
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top