Posts tagged __getattr__
… or how to imitate dynamic properties in a class object
Preface: When you have connections between your application and other systems frequently the data is not in the most useful form for your needs. If you have an API it is awesome but sometimes it just does not act the way you want and your code quickly becomes a series of repeating API calls like api.get_product_property(product_id, property).
Of course it will be easier if you can use objects to represent the data in you code so you can create something like a proxy class to this API:
class Product(object): def __init__(self, product_id): self.id = product_id @property def name(self): return api_obj.get_product_property(self.id, 'name') @property def price(self): return api_obj.get_product_property(self.id, 'price') #usage product = Product(product_id) print product.name
In my opinion it is cleaner, more pretty and more useful than the direct API calls. But still there is something not quite right.
Problem: Your model have not two but twenty properties. Defining 20 method makes the code look not that good. Not to mention that amending the code every time when you need a new property is quite boring. So is there a better way?
As I mention at the end of Connecting Django Models with outer applications if you have a class that plays the role of a proxy to another API or other data it may be easier to overwrite the __getattr__ method.
class Product(object): def __init__(self, product_id): self.id = product_id def __getattr__(self, key): return api_obj.get_product_property(self.id, key) #usage product = Product(product_id) print product.name
Now you can directly use the product properties as attribute names of the Product class. Depending from the way that the API works it would be good to raise AttributeError if there is no such property for the product.