Skip to content
Advertisement

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

Python Decorator, Wrapper got argument multiple times

I wrote this decorator to be used by two functions: I keep getting the error: How can I pass the argument page recursively without having it multiple times? Thanks! EDIT: functions are called as in: Answer Positional arguments in python must be passed in order of definition in the function prototype. This line passes the value of survey_id to the

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 of an attribute, but for useful purposes it

decorating decorators: try to get my head around understanding it

I’m trying to understand how to decorate decorators, and wanted to try out the following: Let’s say I have two decorators and apply them to the function hello(): Then I have to start adding other decorators for other functions, but in general the @wrap decorator will “wrap all of them” How do I write a decorator, which decorates my @lower

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 – Python decorator makes function forget that it belongs to a class and Get class in

How to get method parameter names?

Given that a function a_method has been defined like Starting from a_method itself, how can I get the argument names – for example, as a tuple of strings, like (“arg1”, “arg2”)? Answer Take a look at the inspect module – this will do the inspection of the various code object properties for you. The other results are the name of

Advertisement