mardi 18 avril 2017

Why does __getattribute__ doesn't work with some values?

my question is about getattribute. Please consider the following piece of code:`

class Product():

  def     __init__(self, name, quantity = 1, price =  0.0): = name
    self.quantity = quantity
          self.price = price

  def     __getattribute__(self, attr):
    if (attr == 'price'):
        return 30 * object.__getattribute__(self, attr)
       #change object.blala-la to the or self.quantity 
        return object.__getattribute__(self, attr)

tmp = Product("Watermelon", 2, 50.0);

I have found a bit misleading that if i change the object.__getattribute__(self, attr) to the or self.quantity i will get 30 times string "Watermelon" or 60.0 (in case self.quantity), but if i put there self.price i will get RuntimeError, smth about max recursion depth. So, question: Why i cann't do that with self.price and just write instead object.__getattribute__(self, attr)?


0 commentaires:

Enregistrer un commentaire