Back
Close

Journey to Master Python (WIP)

ageekymonk
34.4K views

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
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io