Skip to content
Advertisement

How to avoid name collisions in python decorators functions

I would like to write a python decorator so that a function raising an exception will be run again until either it succeeds, or it reaches the maximum number of attempts before giving up.

Like so :

JavaScript

My problem is that I want a guarantee that no matter what names the kwargs contain, there cannot be a collision with the name used to denote the number of attempts made.

however this does not work when the function itself takes attempts as a keyword argument

JavaScript

In this example,if other is run, it will run z times and not MAXIMUM times (note that for this bug to happen, the keyword argument must be explicitly used in the call !).

Advertisement

Answer

You can specify decorator parameter, something along the lines of this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement