Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: should decorator names be actions or descriptions?

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)?

like image 335
Claudiu Avatar asked Jun 15 '12 19:06

Claudiu


2 Answers

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.

like image 53
Simeon Visser Avatar answered Oct 04 '22 23:10

Simeon Visser


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.

like image 34
Andrew Clark Avatar answered Oct 04 '22 23:10

Andrew Clark