Python: should decorator names be actions or descriptions?

Let's say I have a decorator that forces the function to print any exceptions and return None if an exception occurs, instead of failing. Assuming this is a good idea, what is the preferred name style?

a)

@ignore_exceptions def foobar(a, b, c): raise ValueError("This function always fails...") 

b)

 @ignores_exceptions def foobar(a, b, c): raise ValueError("This function always fails...") 

That is: should it a) be a command (the decorator tells the function to do something else) or b) description (the decorator allows the programmer to know the attribute of the function)?

+4
source share
2 answers

I think that the active version ( ignore_exceptions ) is used more than the descriptive version ( ignores_exceptions ), at least in the basic Python codes that I am familiar with.

There is a naming convention section in the PEP 8 manual, but in this case it does not offer much help. In any case, consistency between your code base is the most important thing.

+5
source

I would say that ignore_exceptions are better here, simply based on what I see on the PythonDecoratorLibrary page.

Some examples use the names of <decorators> countcalls and dump_args , which is more consistent with ignore_exceptions than ignores_exceptions .

Consistency is the only reason for choosing one over the other, as both make it clear what is happening.

+2
source

All Articles