Skip to content

Tag: decorator

Python try_decorator to retry i2c connections

I am communicating with some hardware over an I2C bus. Every so often, there is an exception as the bus is busy so I am currently handling this with a loop that retries, but there’s a lot of code repetition, I want to tidy it up. So, I tried to write a decorator function to pass various functions to As

Decorating a class to monitor attribute changes

I want to have classes that automatically send notifications to subscribers whenever one of their attributes change. So if I would write this code: The output would be: My question is how to implement the ChangeMonitor decorator class. In the above example I assume it will print a line indicating the changes …

Can a decorator of an instance method access the class?

I have something roughly like the following. Basically I need to access the class of an instance method from a decorator used upon the instance method in its definition. The code as-is gives: AttributeError: ‘function’ object has no attribute ‘im_class’ I found similar question/answers…