Journey to Master Python (WIP)


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Dynamic Attributes

Handling Unknown Instance attributes

You can dynamically respond to get instance attributes. python provides special method called __getattr__ which can be implemented to respond to get attributes

In the below example we are subclassing dict so that we can get elements as class attributes rather than with element accessor.

Dynamic get attributes

Setting unknown instance attributes

Extending the above example we can also set attributes using __setattr__. __setattr__ gets called when there is no such attribute to set. We can implement it for our purpose.

Dynamic set attributes
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content