Say I have a decorator which causes the function to print out any exceptions and return None, if an exception happens, instead of failing. Assuming this is a good idea, what's the preferred naming 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 different), or b)
a description (the decorator lets the progammer know an attribute of the function)?
I think the active version (ignore_exceptions
) is more used than the descriptive version (ignores_exceptions
), at least in the Python code bases that I'm familiar with.
The PEP 8 guideline does have a section on naming conventions but it does not offer much help in this case. In any case, consistency across your code base is the most important thing.
I would say that ignore_exceptions
is better here, simply based on what I am seeing in the PythonDecoratorLibrary page.
Some example decorator names used there are countcalls
and dump_args
, which is more consistent with ignore_exceptions
than ignores_exceptions
.
Consistency is really the only reason to choose one over the other, since both make it clear what is happening.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With