Published at Jan. 30, 2012 | Tagged with: Python , __getattr__ , Development

... 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

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.