Python – what are all the built-in decorators?
Question:
I know of @staticmethod
, @classmethod
, and @property
, but only through scattered documentation. What are all the function decorators that are built into Python? Is that in the docs? Is there an up-to-date list maintained somewhere?
Answers:
I don’t think so. Decorators don’t differ from ordinary functions, you only call them in a fancier way.
For finding all of them try searching Built-in functions list, because as you can see in Python glossary the decorator syntax is just a syntactic sugar, as the following two definitions create equal functions (copied this example from glossary):
def f(...):
...
f = staticmethod(f)
@staticmethod
def f(...):
So any built-in function that returns another function can be used as a decorator. Question is – does it make sense to use it that way? 🙂
functools module contains some functions that can be used as decorators, but they aren’t built-ins you asked for.
They’re not built-in, but this library of example decorators is very good.
As Abgan says, the built-in function list is probably the best place to look. Although, since decorators can also be implemented as classes, it’s not guaranteed to be comprehensive.
There is no such thing as a list of all decorators. There’s no list of all functions. There’s no list of all classes.
Decorators are a handy tool for defining a common aspect across functions, methods, or classes. There are the built-in decorators. Plus there are any number of cool and useless decorators. In the same way there are any number of cool and useless classes.
Decorators aren’t even required to return a function. I’ve used @atexit.register
before.
I know of @staticmethod
, @classmethod
, and @property
, but only through scattered documentation. What are all the function decorators that are built into Python? Is that in the docs? Is there an up-to-date list maintained somewhere?
I don’t think so. Decorators don’t differ from ordinary functions, you only call them in a fancier way.
For finding all of them try searching Built-in functions list, because as you can see in Python glossary the decorator syntax is just a syntactic sugar, as the following two definitions create equal functions (copied this example from glossary):
def f(...):
...
f = staticmethod(f)
@staticmethod
def f(...):
So any built-in function that returns another function can be used as a decorator. Question is – does it make sense to use it that way? 🙂
functools module contains some functions that can be used as decorators, but they aren’t built-ins you asked for.
They’re not built-in, but this library of example decorators is very good.
As Abgan says, the built-in function list is probably the best place to look. Although, since decorators can also be implemented as classes, it’s not guaranteed to be comprehensive.
There is no such thing as a list of all decorators. There’s no list of all functions. There’s no list of all classes.
Decorators are a handy tool for defining a common aspect across functions, methods, or classes. There are the built-in decorators. Plus there are any number of cool and useless decorators. In the same way there are any number of cool and useless classes.
Decorators aren’t even required to return a function. I’ve used @atexit.register
before.