What does the “at” (@) symbol do in Python?

前端 未结 12 1138
萌比男神i
萌比男神i 2020-11-22 01:57

I\'m looking at some Python code which used the @ symbol, but I have no idea what it does. I also do not know what to search for as searching Python docs or Goo

12条回答
  •  甜味超标
    2020-11-22 02:46

    To say what others have in a different way: yes, it is a decorator.

    In Python, it's like:

    1. Creating a function (follows under the @ call)
    2. Calling another function to operate on your created function. This returns a new function. The function that you call is the argument of the @.
    3. Replacing the function defined with the new function returned.

    This can be used for all kinds of useful things, made possible because functions are objects and just necessary just instructions.

提交回复
热议问题