Python: if more than one of three things is true, return false

前端 未结 11 2513
温柔的废话
温柔的废话 2021-02-14 01:33

I\'m writing a django model that allows my site to have coupons.

Coupons can have three types: lifetime account voucher, certain period of months voucher, certain numb

11条回答
  •  盖世英雄少女心
    2021-02-14 01:50

    I think spreading this over a few lines is fine - this makes it easier to maintain if there were more attributes to test in the future. Using len or sum feels a bit too obfuscated

    # Ensure that only one of these values is set
    true_count = 0
    true_count += bool(self.months)
    true_count += bool(self.dollars)
    true_count += bool(self.lifetime)
    

提交回复
热议问题